Skip to content

Use OkHSV for RichTextLabel rainbows #106733

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions core/math/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,19 @@ void Color::set_ok_hsl(float p_h, float p_s, float p_l, float p_alpha) {
a = c.a;
}

void Color::set_ok_hsv(float p_h, float p_s, float p_v, float p_alpha) {
ok_color::HSV hsv;
hsv.h = p_h;
hsv.s = p_s;
hsv.v = p_v;
ok_color::RGB rgb = ok_color::okhsv_to_srgb(hsv);
Color c = Color(rgb.r, rgb.g, rgb.b, p_alpha).clamp();
r = c.r;
g = c.g;
b = c.b;
a = c.a;
}

bool Color::is_equal_approx(const Color &p_color) const {
return Math::is_equal_approx(r, p_color.r) && Math::is_equal_approx(g, p_color.g) && Math::is_equal_approx(b, p_color.b) && Math::is_equal_approx(a, p_color.a);
}
Expand Down Expand Up @@ -476,6 +489,12 @@ Color Color::from_ok_hsl(float p_h, float p_s, float p_l, float p_alpha) {
return c;
}

Color Color::from_ok_hsv(float p_h, float p_s, float p_l, float p_alpha) {
Color c;
c.set_ok_hsv(p_h, p_s, p_l, p_alpha);
return c;
}

float Color::get_ok_hsl_h() const {
ok_color::RGB rgb;
rgb.r = r;
Expand Down
2 changes: 2 additions & 0 deletions core/math/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ struct [[nodiscard]] Color {
float get_ok_hsl_s() const;
float get_ok_hsl_l() const;
void set_ok_hsl(float p_h, float p_s, float p_l, float p_alpha = 1.0f);
void set_ok_hsv(float p_h, float p_s, float p_v, float p_alpha = 1.0f);

_FORCE_INLINE_ float &operator[](int p_idx) {
return components[p_idx];
Expand Down Expand Up @@ -214,6 +215,7 @@ struct [[nodiscard]] Color {
static Color from_string(const String &p_string, const Color &p_default);
static Color from_hsv(float p_h, float p_s, float p_v, float p_alpha = 1.0f);
static Color from_ok_hsl(float p_h, float p_s, float p_l, float p_alpha = 1.0f);
static Color from_ok_hsv(float p_h, float p_s, float p_l, float p_alpha = 1.0f);
static Color from_rgbe9995(uint32_t p_rgbe);
static Color from_rgba8(int64_t p_r8, int64_t p_g8, int64_t p_b8, int64_t p_a8 = 255);

Expand Down
2 changes: 1 addition & 1 deletion scene/gui/rich_text_label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1296,7 +1296,7 @@ int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_o
} else if (item_fx->type == ITEM_RAINBOW) {
ItemRainbow *item_rainbow = static_cast<ItemRainbow *>(item_fx);

font_color = font_color.from_hsv(MAX(item_rainbow->frequency, 0) * Math::abs(item_rainbow->elapsed_time * item_rainbow->speed + ((p_ofs.x + off_step.x) / 50)), item_rainbow->saturation, item_rainbow->value, font_color.a);
font_color = font_color.from_ok_hsv(MAX(item_rainbow->frequency, 0) * Math::abs(item_rainbow->elapsed_time * item_rainbow->speed + ((p_ofs.x + off_step.x) / 50)), item_rainbow->saturation, item_rainbow->value, font_color.a);
} else if (item_fx->type == ITEM_PULSE) {
ItemPulse *item_pulse = static_cast<ItemPulse *>(item_fx);

Expand Down
4 changes: 2 additions & 2 deletions scene/gui/rich_text_label.h
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,8 @@ class RichTextLabel : public Control {
};

struct ItemRainbow : public ItemFX {
float saturation = 0.8f;
float value = 0.8f;
float saturation = 0.9f;
float value = 1.0f;
float frequency = 1.0f;
float speed = 1.0f;

Expand Down