mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-09-28 16:29:34 +08:00
Feature: LinearGradient
color decorator. (#592)
Based on the existing color decorators, create new ones to apply a gradient effect on the DOM. Co-authored-by: ArthurSonzogni <sonzogniarthur@gmail.com>
This commit is contained in:

committed by
GitHub

parent
2991b0389e
commit
e83e90ced2
@@ -1,6 +1,7 @@
|
||||
#include "ftxui/screen/color.hpp"
|
||||
|
||||
#include <array> // for array
|
||||
#include <array> // for array
|
||||
#include <cmath>
|
||||
#include <string_view> // for literals
|
||||
|
||||
#include "ftxui/screen/color_info.hpp" // for GetColorInfo, ColorInfo
|
||||
@@ -220,12 +221,12 @@ Color Color::Interpolate(float t, const Color& a, const Color& b) {
|
||||
get_color(a, &red_a, &green_a, &blue_a);
|
||||
get_color(b, &red_b, &green_b, &blue_b);
|
||||
|
||||
return Color::RGB(static_cast<uint8_t>(static_cast<float>(red_a) * (1 - t) +
|
||||
static_cast<float>(red_b) * t),
|
||||
static_cast<uint8_t>(static_cast<float>(green_a) * (1 - t) +
|
||||
static_cast<float>(green_b) * t),
|
||||
static_cast<uint8_t>(static_cast<float>(blue_a) * (1 - t) +
|
||||
static_cast<float>(blue_b) * t));
|
||||
// Gamma correction:
|
||||
// https://en.wikipedia.org/wiki/Gamma_correction
|
||||
return Color::RGB(
|
||||
pow(pow(red_a, 2.2f) * (1 - t) + pow(red_b, 2.2f) * t, 1 / 2.2f),
|
||||
pow(pow(green_a, 2.2f) * (1 - t) + pow(green_b, 2.2f) * t, 1 / 2.2f),
|
||||
pow(pow(blue_a, 2.2f) * (1 - t) + pow(blue_b, 2.2f) * t, 1 / 2.2f));
|
||||
}
|
||||
|
||||
inline namespace literals {
|
||||
|
@@ -57,22 +57,22 @@ TEST(ColorTest, Interpolate) {
|
||||
Color::RGB(1, 2, 3), //
|
||||
Color::RGB(244, 244, 123)) //
|
||||
.Print(false),
|
||||
"38;2;73;74;39");
|
||||
"38;2;141;141;71");
|
||||
EXPECT_EQ(Color::Interpolate(0.7f, //
|
||||
Color::RGB(1, 2, 3), //
|
||||
Color::RGB(244, 244, 123)) //
|
||||
.Print(false),
|
||||
"38;2;171;171;87");
|
||||
"38;2;207;207;104");
|
||||
EXPECT_EQ(Color::Interpolate(0.7f, //
|
||||
Color(Color::Red), //
|
||||
Color::RGB(244, 244, 123)) //
|
||||
.Print(false),
|
||||
"38;2;209;170;86");
|
||||
"38;2;216;207;104");
|
||||
EXPECT_EQ(Color::Interpolate(0.7f, //
|
||||
Color::RGB(244, 244, 123), //
|
||||
Color(Color::Plum1)) //
|
||||
.Print(false),
|
||||
"38;2;251;195;215");
|
||||
"38;2;251;198;225");
|
||||
}
|
||||
|
||||
TEST(ColorTest, HSV) {
|
||||
|
Reference in New Issue
Block a user