diff --git a/src/tui/screen/colors.cppm b/src/tui/screen/colors.cppm index 53daf51..9fee0d0 100644 --- a/src/tui/screen/colors.cppm +++ b/src/tui/screen/colors.cppm @@ -635,7 +635,7 @@ constexpr auto palette256_table = std::array{ /** * @brief 获取 16 种预设的颜色的对应的信息 */ -constexpr auto get_color_info(palette16 index) noexcept -> color_info +constexpr auto get_color_info(palette16 index) noexcept -> const color_info& { return palette256_table[std::to_underlying(index)]; } @@ -643,11 +643,35 @@ constexpr auto get_color_info(palette16 index) noexcept -> color_info /** * @brief 获取 256 种预设的颜色的对应的信息 */ -constexpr auto get_color_info(palette256 index) noexcept -> color_info +constexpr auto get_color_info(palette256 index) noexcept -> const color_info& { return palette256_table[std::to_underlying(index)]; } +/** + * @brief 从 RGB 值获取最接近的 256 种预设颜色 + */ +constexpr auto get_nearest_palette256(rgb_t rgb) noexcept -> palette256 +{ + auto p256 = std::views::all(palette256_table) | std::views::drop(16uz); + auto it = std::ranges::min_element(p256, {}, [rgb](const color_info& c) { + auto dr = c.red - rgb.red; + auto dg = c.green - rgb.green; + auto db = c.blue - rgb.blue; + return dr * dr + dg * dg + db * db; + }); + return static_cast(it->index256); +} + +/** + * @brief 从 256 种预设颜色获取最接近的 16 种预设颜色 + */ +constexpr auto get_nearest_palette16(palette256 c) noexcept -> palette16 +{ + auto&& p16 = get_color_info(c); + return static_cast(p16.index16); +} + } }