FTXUI 6.1.9 C++ functional terminal UI.

Loading...
Searching...
No Matches
color_info_sorted_2d.ipp
Go to the documentation of this file.
1#include <algorithm>
2#include <cmath>
3#include <ftxui/screen/color_info.hpp> // for ftxui::ColorInfo
4
5std::vector<std::vector<ftxui::ColorInfo>> ColorInfoSorted2D() {
6 // Acquire the color information for the palette256.
7 std::vector<ftxui::ColorInfo> info_gray;
8 std::vector<ftxui::ColorInfo> info_color;
9 for (int i = 16; i < 256; ++i) {
10 ftxui::ColorInfo info = GetColorInfo(ftxui::Color::Palette256(i));
11 if (info.saturation == 0)
12 info_gray.push_back(info);
13 else
14 info_color.push_back(info);
15 }
16
17 // Sort info_color by hue.
18 std::sort(
19 info_color.begin(), info_color.end(),
20 [](const ftxui::ColorInfo& A, const ftxui::ColorInfo& B) { return A.hue < B.hue; });
21
22 // Make 8 colums, one gray and seven colored.
23 std::vector<std::vector<ftxui::ColorInfo>> info_columns(8);
24 info_columns[0] = info_gray;
25 for (size_t i = 0; i < info_color.size(); ++i) {
26 info_columns[1 + 7 * i / info_color.size()].push_back(info_color[i]);
27 }
28
29 // Minimize discontinuities for every columns.
30 for (auto& column : info_columns) {
31 std::sort(column.begin(), column.end(),
32 [](const ftxui::ColorInfo& A, const ftxui::ColorInfo& B) {
33 return A.value < B.value;
34 });
35 for (int i = 0; i < int(column.size()) - 1; ++i) {
36 int best_index = i + 1;
37 int best_distance = 255 * 255 * 3;
38 for (int j = i + 1; j < column.size(); ++j) {
39 int dx = column[i].red - column[j].red;
40 int dy = column[i].green - column[j].green;
41 int dz = column[i].blue - column[j].blue;
42 int distance = dx * dx + dy * dy + dz * dz;
43 if (best_distance > distance) {
44 best_distance = distance;
45 best_index = j;
46 }
47 }
48 std::swap(column[i + 1], column[best_index]);
49 }
50 }
51
52 return std::move(info_columns);
53}
color_info.hpp
ColorInfoSorted2D
std::vector< std::vector< ftxui::ColorInfo > > ColorInfoSorted2D()
Definition color_info_sorted_2d.ipp:5
ftxui::ColorInfo::saturation
uint8_t saturation
Definition color_info.hpp:24
ftxui::Color::Palette256
Palette256
Definition color.hpp:72
ftxui::ColorInfo
ColorInfo is a structure that contains information about the terminal color palette.
Definition color_info.hpp:16