From a84d1be86db2a3a783d9989e926d1399c78ff96c Mon Sep 17 00:00:00 2001 From: keqingmoe Date: Wed, 11 Dec 2024 14:29:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=8A=9F=E8=83=BD=EF=BC=9A?= =?UTF-8?q?=E5=BD=93=E7=BB=88=E7=AB=AF=E4=B8=8D=E6=94=AF=E6=8C=81=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E5=8F=AF=E4=BB=A5=E8=BD=AC=E4=B8=BA=E6=9C=80=E6=8E=A5?= =?UTF-8?q?=E8=BF=91=E7=9A=84=E5=8F=97=E6=94=AF=E6=8C=81=E7=9A=84=E9=A2=9C?= =?UTF-8?q?=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/tui/screen/colors.cppm | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) 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); +} + } }