mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-09-16 16:08:08 +08:00
Improve color handling. (#49)
This commit is contained in:
@@ -1,25 +1,21 @@
|
||||
#include <cmath>
|
||||
#include <ftxui/dom/elements.hpp>
|
||||
#include <ftxui/screen/color_info.hpp>
|
||||
#include <ftxui/screen/screen.hpp>
|
||||
#include <ftxui/screen/terminal.hpp>
|
||||
#include <iostream>
|
||||
|
||||
#include "ftxui/screen/string.hpp"
|
||||
|
||||
int main(int argc, const char* argv[]) {
|
||||
using namespace ftxui;
|
||||
// clang-format off
|
||||
auto terminal_info =
|
||||
vbox(
|
||||
text(L"Basic Color support : unknown"),
|
||||
text(L"256 Color support : unknown"),
|
||||
(Terminal::CanSupportTrueColors() ?
|
||||
text(L"TrueColor support : Yes"):
|
||||
text(L"TrueColor support : No"))
|
||||
);
|
||||
using namespace ftxui;
|
||||
#include "./color_info_sorted_2d.ipp" // ColorInfoSorted2D.
|
||||
|
||||
int main(int argc, const char* argv[]) {
|
||||
// clang-format off
|
||||
auto basic_color_display =
|
||||
vbox(
|
||||
text(L"Basic Color Set:"),
|
||||
text(L"16 color palette:"),
|
||||
separator(),
|
||||
hbox(
|
||||
vbox(
|
||||
color(Color::Default, text(L"Default")),
|
||||
@@ -63,37 +59,68 @@ int main(int argc, const char* argv[]) {
|
||||
);
|
||||
|
||||
// clang-format on
|
||||
auto palette_256_color_display = vbox(text(L"256 color palette:"));
|
||||
|
||||
int y = -1;
|
||||
for (int i = 0; i < 256; ++i) {
|
||||
if (i % 16 == 0) {
|
||||
palette_256_color_display->children.push_back(hbox());
|
||||
++y;
|
||||
auto palette_256_color_display = text(L"256 colors palette:");
|
||||
{
|
||||
std::vector<std::vector<ColorInfo>> info_columns = ColorInfoSorted2D();
|
||||
Elements columns;
|
||||
for (auto& column : info_columns) {
|
||||
Elements column_elements;
|
||||
for (auto& it : column) {
|
||||
column_elements.push_back(
|
||||
text(L" ") | bgcolor(Color(Color::Palette256(it.index_256))));
|
||||
}
|
||||
columns.push_back(hbox(std::move(column_elements)));
|
||||
}
|
||||
std::string number = std::to_string(i);
|
||||
while (number.length() < 4) {
|
||||
number.push_back(' ');
|
||||
}
|
||||
palette_256_color_display->children.back()->children.push_back(
|
||||
bgcolor(Color::Palette256(i), text(to_wstring(number))));
|
||||
palette_256_color_display = vbox({
|
||||
palette_256_color_display,
|
||||
separator(),
|
||||
vbox(columns),
|
||||
});
|
||||
}
|
||||
|
||||
auto true_color_display = vbox(text(L"a true color grandient:"));
|
||||
|
||||
for (int i = 0; i < 17; ++i) {
|
||||
true_color_display->children.push_back(hbox());
|
||||
|
||||
for (int j = 0; j < 30; ++j) {
|
||||
true_color_display->children.back()->children.push_back(
|
||||
bgcolor(Color(50 + i * 5, 100 + j, 150), text(L" ")));
|
||||
// True color display.
|
||||
auto true_color_display = text(L"TrueColors: 24bits:");
|
||||
{
|
||||
int saturation = 255;
|
||||
Elements array;
|
||||
for (int value = 0; value < 255; value += 16) {
|
||||
Elements line;
|
||||
for (int hue = 0; hue < 255; hue += 6) {
|
||||
line.push_back(text(L"▀") //
|
||||
| color(Color::HSV(hue, saturation, value)) //
|
||||
| bgcolor(Color::HSV(hue, saturation, value + 8)));
|
||||
}
|
||||
array.push_back(hbox(std::move(line)));
|
||||
}
|
||||
true_color_display = vbox({
|
||||
true_color_display,
|
||||
separator(),
|
||||
vbox(std::move(array)),
|
||||
});
|
||||
}
|
||||
|
||||
auto document =
|
||||
vbox(terminal_info, text(L""),
|
||||
hbox(basic_color_display, text(L" "), palette_256_color_display,
|
||||
text(L" "), true_color_display));
|
||||
auto terminal_info =
|
||||
vbox({
|
||||
Terminal::ColorSupport() >= Terminal::Color::Palette16
|
||||
? text(L" 16 color palette support : Yes")
|
||||
: text(L" 16 color palette support : No"),
|
||||
Terminal::ColorSupport() >= Terminal::Color::Palette256
|
||||
? text(L"256 color palette support : Yes")
|
||||
: text(L"256 color palette support : No"),
|
||||
Terminal::ColorSupport() >= Terminal::Color::TrueColor
|
||||
? text(L" True color support : Yes")
|
||||
: text(L" True color support : No"),
|
||||
}) |
|
||||
border;
|
||||
|
||||
auto document = vbox({hbox({
|
||||
basic_color_display,
|
||||
text(L" "),
|
||||
palette_256_color_display,
|
||||
text(L" "),
|
||||
true_color_display,
|
||||
}),
|
||||
terminal_info});
|
||||
// clang-format on
|
||||
|
||||
auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
|
||||
|
@@ -1,4 +1,5 @@
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <ftxui/dom/elements.hpp>
|
||||
#include <ftxui/screen/color_info.hpp>
|
||||
#include <ftxui/screen/screen.hpp>
|
||||
@@ -6,54 +7,11 @@
|
||||
#include <iostream>
|
||||
#include "ftxui/screen/string.hpp"
|
||||
|
||||
using namespace ftxui;
|
||||
#include "./color_info_sorted_2d.ipp" // ColorInfoSorted2D.
|
||||
|
||||
int main(int argc, const char* argv[]) {
|
||||
using namespace ftxui;
|
||||
|
||||
// Acquire the color information for the palette256.
|
||||
std::vector<ColorInfo> info_gray;
|
||||
std::vector<ColorInfo> info_color;
|
||||
for (int i = 0; i < 256; ++i) {
|
||||
ColorInfo info = GetColorInfo(Color::Palette256(i));
|
||||
if (info.saturation == 0)
|
||||
info_gray.push_back(info);
|
||||
else
|
||||
info_color.push_back(info);
|
||||
}
|
||||
|
||||
// Sort info_color by hue.
|
||||
std::sort(
|
||||
info_color.begin(), info_color.end(),
|
||||
[](const ColorInfo& A, const ColorInfo& B) { return A.hue < B.hue; });
|
||||
|
||||
// Make 8 colums, one gray and seven colored.
|
||||
std::vector<std::vector<ColorInfo>> info_columns(8);
|
||||
info_columns[0] = info_gray;
|
||||
for (int i = 0; i < info_color.size(); ++i) {
|
||||
info_columns[1 + 7 * i / info_color.size()].push_back(info_color[i]);
|
||||
}
|
||||
|
||||
// Minimize discontinuities for every columns.
|
||||
for (auto& column : info_columns) {
|
||||
std::sort(column.begin(), column.end(),
|
||||
[](const ColorInfo& A, const ColorInfo& B) {
|
||||
return A.value < B.value;
|
||||
});
|
||||
for (int i = 0; i < column.size() - 1; ++i) {
|
||||
int best_index = i + 1;
|
||||
int best_distance = 255 * 255 * 3;
|
||||
for (int j = i + 1; j < column.size(); ++j) {
|
||||
int dx = column[i].red - column[j].red;
|
||||
int dy = column[i].green - column[j].green;
|
||||
int dz = column[i].blue - column[j].blue;
|
||||
int distance = dx * dx + dy * dy + dz * dz;
|
||||
if (best_distance > distance) {
|
||||
best_distance = distance;
|
||||
best_index = j;
|
||||
}
|
||||
}
|
||||
std::swap(column[i + 1], column[best_index]);
|
||||
}
|
||||
}
|
||||
std::vector<std::vector<ColorInfo>> info_columns = ColorInfoSorted2D();
|
||||
|
||||
// Draw every columns
|
||||
Elements columns_elements;
|
||||
@@ -61,7 +19,7 @@ int main(int argc, const char* argv[]) {
|
||||
Elements column_elements;
|
||||
for (auto& it : column) {
|
||||
column_elements.push_back(hbox({
|
||||
text(L" ") | bgcolor(Color(Color::Palette256(it.index))),
|
||||
text(L" ") | bgcolor(Color(Color::Palette256(it.index_256))),
|
||||
text(to_wstring(std::string(it.name))),
|
||||
}));
|
||||
}
|
||||
|
52
examples/dom/color_info_sorted_2d.ipp
Normal file
52
examples/dom/color_info_sorted_2d.ipp
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
|
||||
std::vector<std::vector<ColorInfo>> ColorInfoSorted2D() {
|
||||
// Acquire the color information for the palette256.
|
||||
std::vector<ColorInfo> info_gray;
|
||||
std::vector<ColorInfo> info_color;
|
||||
for (int i = 16; i < 256; ++i) {
|
||||
ColorInfo info = GetColorInfo(Color::Palette256(i));
|
||||
if (info.saturation == 0)
|
||||
info_gray.push_back(info);
|
||||
else
|
||||
info_color.push_back(info);
|
||||
}
|
||||
|
||||
// Sort info_color by hue.
|
||||
std::sort(
|
||||
info_color.begin(), info_color.end(),
|
||||
[](const ColorInfo& A, const ColorInfo& B) { return A.hue < B.hue; });
|
||||
|
||||
// Make 8 colums, one gray and seven colored.
|
||||
std::vector<std::vector<ColorInfo>> info_columns(8);
|
||||
info_columns[0] = info_gray;
|
||||
for (int i = 0; i < info_color.size(); ++i) {
|
||||
info_columns[1 + 7 * i / info_color.size()].push_back(info_color[i]);
|
||||
}
|
||||
|
||||
// Minimize discontinuities for every columns.
|
||||
for (auto& column : info_columns) {
|
||||
std::sort(column.begin(), column.end(),
|
||||
[](const ColorInfo& A, const ColorInfo& B) {
|
||||
return A.value < B.value;
|
||||
});
|
||||
for (int i = 0; i < column.size() - 1; ++i) {
|
||||
int best_index = i + 1;
|
||||
int best_distance = 255 * 255 * 3;
|
||||
for (int j = i + 1; j < column.size(); ++j) {
|
||||
int dx = column[i].red - column[j].red;
|
||||
int dy = column[i].green - column[j].green;
|
||||
int dz = column[i].blue - column[j].blue;
|
||||
int distance = dx * dx + dy * dy + dz * dz;
|
||||
if (best_distance > distance) {
|
||||
best_distance = distance;
|
||||
best_index = j;
|
||||
}
|
||||
}
|
||||
std::swap(column[i + 1], column[best_index]);
|
||||
}
|
||||
}
|
||||
|
||||
return std::move(info_columns);
|
||||
}
|
@@ -1,3 +1,4 @@
|
||||
#include <cmath>
|
||||
#include <ftxui/dom/elements.hpp>
|
||||
#include <ftxui/screen/screen.hpp>
|
||||
#include <ftxui/screen/terminal.hpp>
|
||||
@@ -10,10 +11,13 @@ int main(int argc, const char* argv[]) {
|
||||
|
||||
int saturation = 255;
|
||||
Elements array;
|
||||
for (int value = 0; value < 255; value += 10) {
|
||||
for (int value = 0; value < 255; value += 20) {
|
||||
Elements line;
|
||||
for (int hue = 0; hue < 255; hue += 2)
|
||||
line.push_back(text(L" ") | bgcolor(Color::HSV(hue, saturation, value)));
|
||||
for (int hue = 0; hue < 255; hue += 2) {
|
||||
line.push_back(text(L"▀") //
|
||||
| color(Color::HSV(hue, saturation, value)) //
|
||||
| bgcolor(Color::HSV(hue, saturation, value + 10)));
|
||||
}
|
||||
array.push_back(hbox(std::move(line)));
|
||||
}
|
||||
|
||||
|
@@ -2,12 +2,21 @@
|
||||
#include <ftxui/screen/screen.hpp>
|
||||
#include <iostream>
|
||||
|
||||
int main(int argc, const char* argv[]) {
|
||||
int main(void) {
|
||||
using namespace ftxui;
|
||||
auto document = window(text(L"Title"), text(L"content"));
|
||||
auto screen = Screen::Create(Dimension::Fixed(30), Dimension::Fixed(6));
|
||||
Element document = graph([](int x, int y) {
|
||||
std::vector<int> result(x, 0);
|
||||
for (int i{0}; i < x; ++i) {
|
||||
result[i] = ((3 * i) / 2) % y;
|
||||
}
|
||||
return result;
|
||||
}) |
|
||||
color(Color::Red) | border | color(Color::Green) |
|
||||
bgcolor(Color::DarkBlue);
|
||||
|
||||
auto screen = Screen::Create(Dimension::Fixed(80), Dimension::Fixed(10));
|
||||
Render(screen, document);
|
||||
std::cout << screen.ToString() << std::endl;
|
||||
std::cout << screen.ToString() << '\n';
|
||||
}
|
||||
|
||||
// Copyright 2020 Arthur Sonzogni. All rights reserved.
|
||||
|
Reference in New Issue
Block a user