mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-09-19 18:18:09 +08:00
@@ -53,12 +53,12 @@ std::string Color::Print(bool is_background_color) const {
|
||||
return (is_background_color ? "48;5;"s : "38;5;"s) + std::to_string(red_);
|
||||
|
||||
case ColorType::TrueColor:
|
||||
default:
|
||||
return (is_background_color ? "48;2;"s : "38;2;"s) //
|
||||
+ std::to_string(red_) + ";" //
|
||||
+ std::to_string(green_) + ";" //
|
||||
+ std::to_string(blue_); //
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/// @brief Build a transparent color.
|
||||
@@ -96,7 +96,7 @@ Color::Color(uint8_t red, uint8_t green, uint8_t blue)
|
||||
return;
|
||||
}
|
||||
|
||||
// Find the closest coor from the database:
|
||||
// Find the closest Color from the database:
|
||||
const int max_distance = 256 * 256 * 3;
|
||||
int closest = max_distance;
|
||||
int best = 0;
|
||||
|
47
src/ftxui/screen/color_test.cpp
Normal file
47
src/ftxui/screen/color_test.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include "ftxui/screen/color.hpp"
|
||||
#include <gtest/gtest-message.h> // for Message
|
||||
#include <gtest/gtest-test-part.h> // for TestPartResult, SuiteApiResolver, TestFactoryImpl
|
||||
#include "ftxui/screen/terminal.hpp"
|
||||
#include "gtest/gtest_pred_impl.h" // for EXPECT_EQ, Test, TEST
|
||||
|
||||
namespace ftxui {
|
||||
|
||||
TEST(ColorTest, PrintTransparent) {
|
||||
Terminal::SetColorSupport(Terminal::Color::TrueColor);
|
||||
EXPECT_EQ(Color().Print(false), "39");
|
||||
EXPECT_EQ(Color().Print(true), "49");
|
||||
}
|
||||
|
||||
TEST(ColorTest, PrintColor16) {
|
||||
Terminal::SetColorSupport(Terminal::Color::TrueColor);
|
||||
EXPECT_EQ(Color(Color::Red).Print(false), "31");
|
||||
EXPECT_EQ(Color(Color::Red).Print(true), "41");
|
||||
}
|
||||
|
||||
TEST(ColorTest, PrintColor256) {
|
||||
Terminal::SetColorSupport(Terminal::Color::TrueColor);
|
||||
EXPECT_EQ(Color(Color::DarkRed).Print(false), "38;5;52");
|
||||
EXPECT_EQ(Color(Color::DarkRed).Print(true), "48;5;52");
|
||||
}
|
||||
|
||||
TEST(ColorTest, PrintTrueCOlor) {
|
||||
Terminal::SetColorSupport(Terminal::Color::TrueColor);
|
||||
EXPECT_EQ(Color::RGB(1,2,3).Print(false), "38;2;1;2;3");
|
||||
EXPECT_EQ(Color::RGB(1,2,3).Print(true), "48;2;1;2;3");
|
||||
}
|
||||
|
||||
TEST(ColorTest, FallbackTo256) {
|
||||
Terminal::SetColorSupport(Terminal::Color::Palette256);
|
||||
EXPECT_EQ(Color::RGB(1,2,3).Print(false), "38;5;16");
|
||||
}
|
||||
|
||||
TEST(ColorTest, FallbackTo16) {
|
||||
Terminal::SetColorSupport(Terminal::Color::Palette16);
|
||||
EXPECT_EQ(Color::RGB(1,2,3).Print(false), "30");
|
||||
}
|
||||
|
||||
} // namespace ftxui
|
||||
|
||||
// Copyright 2022 Arthur Sonzogni. All rights reserved.
|
||||
// Use of this source code is governed by the MIT license that can be found in
|
||||
// the LICENSE file.
|
@@ -19,6 +19,10 @@
|
||||
namespace ftxui {
|
||||
|
||||
namespace {
|
||||
|
||||
bool g_cached = false;
|
||||
Terminal::Color g_cached_supported_color;
|
||||
|
||||
Dimensions& FallbackSize() {
|
||||
#if defined(__EMSCRIPTEN__)
|
||||
// This dimension was chosen arbitrarily to be able to display:
|
||||
@@ -76,7 +80,8 @@ Terminal::Color ComputeColorSupport() {
|
||||
|
||||
} // namespace
|
||||
|
||||
Dimensions Terminal::Size() {
|
||||
namespace Terminal {
|
||||
Dimensions Size() {
|
||||
#if defined(__EMSCRIPTEN__)
|
||||
// This dimension was chosen arbitrarily to be able to display:
|
||||
// https://arthursonzogni.com/FTXUI/examples
|
||||
@@ -106,20 +111,24 @@ Dimensions Terminal::Size() {
|
||||
|
||||
/// @brief Override terminal size in case auto-detection fails
|
||||
/// @param fallbackSize Terminal dimensions to fallback to
|
||||
void Terminal::SetFallbackSize(const Dimensions& fallbackSize) {
|
||||
void SetFallbackSize(const Dimensions& fallbackSize) {
|
||||
FallbackSize() = fallbackSize;
|
||||
}
|
||||
|
||||
Terminal::Color Terminal::ColorSupport() {
|
||||
static bool cached = false;
|
||||
static Terminal::Color cached_supported_color;
|
||||
if (!cached) {
|
||||
cached = true;
|
||||
cached_supported_color = ComputeColorSupport();
|
||||
Color ColorSupport() {
|
||||
if (!g_cached) {
|
||||
g_cached = true;
|
||||
g_cached_supported_color = ComputeColorSupport();
|
||||
}
|
||||
return cached_supported_color;
|
||||
return g_cached_supported_color;
|
||||
}
|
||||
|
||||
void SetColorSupport(Color color) {
|
||||
g_cached = true;
|
||||
g_cached_supported_color = color;
|
||||
}
|
||||
|
||||
} // namespace Terminal
|
||||
} // namespace ftxui
|
||||
|
||||
// Copyright 2020 Arthur Sonzogni. All rights reserved.
|
||||
|
Reference in New Issue
Block a user