Skip to content

Commit

Permalink
Update ColorPicker
Browse files Browse the repository at this point in the history
  • Loading branch information
WhalesState committed Dec 19, 2024
1 parent d7aa6c2 commit 221143e
Show file tree
Hide file tree
Showing 4 changed files with 769 additions and 1,651 deletions.
96 changes: 50 additions & 46 deletions scene/gui/color_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,21 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

/**************************************************************************/
/* PIXEL ENGINE */
/* Copyright (c) 2024-present Pixel Engine contributors (see AUTHORS.md). */
/**************************************************************************/
/* NOTICE: */
/* This file contains modifications and additions specific to the Pixel */
/* Engine project. While these changes are licensed under the MIT license */
/* for compatibility, we request proper attribution if reused in any */
/* derivative works, including meta-forks. */
/**************************************************************************/

#include "color_mode.h"

#include "core/math/color.h"
#include "scene/gui/color_picker.h"
#include "scene/gui/slider.h"
#include "thirdparty/misc/ok_color.h"

Expand All @@ -45,8 +57,7 @@ String ColorModeRGB::get_slider_label(int idx) const {

float ColorModeRGB::get_slider_max(int idx) const {
ERR_FAIL_INDEX_V_MSG(idx, 4, 0, "Couldn't get slider max value.");
Color color = color_picker->get_pick_color();
return next_power_of_2(MAX(255, color.components[idx] * 255.0)) - 1;
return slider_max[idx];
}

float ColorModeRGB::get_slider_value(int idx) const {
Expand All @@ -73,10 +84,9 @@ void ColorModeRGB::slider_draw(int p_which) {
Color left_color;
Color right_color;
Color color = color_picker->get_pick_color();
const real_t margin = 16 * color_picker->theme_cache.base_scale;

if (p_which == ColorPicker::SLIDER_COUNT) {
slider->draw_texture_rect(color_picker->theme_cache.sample_bg, Rect2(Point2(0, 0), Size2(size.x, margin)), true);
slider->draw_texture_rect(color_picker->theme_cache.sample_bg, Rect2(Point2(), size), true);

left_color = color;
left_color.a = 0;
Expand All @@ -97,10 +107,10 @@ void ColorModeRGB::slider_draw(int p_which) {
col.set(1, right_color);
col.set(2, right_color);
col.set(3, left_color);
pos.set(0, Vector2(0, 0));
pos.set(0, Vector2());
pos.set(1, Vector2(size.x, 0));
pos.set(2, Vector2(size.x, margin));
pos.set(3, Vector2(0, margin));
pos.set(2, size);
pos.set(3, Vector2(0, size.y));

slider->draw_polygon(pos, col);
}
Expand Down Expand Up @@ -168,10 +178,9 @@ void ColorModeHSV::slider_draw(int p_which) {
Color left_color;
Color right_color;
Color color = color_picker->get_pick_color();
const real_t margin = 16 * color_picker->theme_cache.base_scale;

if (p_which == ColorPicker::SLIDER_COUNT) {
slider->draw_texture_rect(color_picker->theme_cache.sample_bg, Rect2(Point2(0, 0), Size2(size.x, margin)), true);
slider->draw_texture_rect(color_picker->theme_cache.sample_bg, Rect2(Point2(), size), true);

left_color = color;
left_color.a = 0;
Expand All @@ -196,16 +205,16 @@ void ColorModeHSV::slider_draw(int p_which) {
col.set(1, right_color);
col.set(2, right_color);
col.set(3, left_color);
pos.set(0, Vector2(0, 0));
pos.set(0, Vector2());
pos.set(1, Vector2(size.x, 0));
pos.set(2, Vector2(size.x, margin));
pos.set(3, Vector2(0, margin));
pos.set(2, size);
pos.set(3, Vector2(0, size.y));

slider->draw_polygon(pos, col);

if (p_which == 0) { // H
Ref<Texture2D> hue = color_picker->theme_cache.color_hue;
slider->draw_texture_rect(hue, Rect2(Vector2(), Vector2(size.x, margin)), false, Color::from_hsv(0, 0, color.get_v(), color.get_s()));
slider->draw_texture_rect(hue, Rect2(Vector2(), size), false, Color::from_hsv(0, 0, color.get_v(), color.get_s()));
}
}

Expand Down Expand Up @@ -243,39 +252,35 @@ void ColorModeRAW::slider_draw(int p_which) {
Color left_color;
Color right_color;
Color color = color_picker->get_pick_color();
const real_t margin = 16 * color_picker->theme_cache.base_scale;

if (p_which == ColorPicker::SLIDER_COUNT) {
slider->draw_texture_rect(color_picker->theme_cache.sample_bg, Rect2(Point2(0, 0), Size2(size.x, margin)), true);
slider->draw_texture_rect(color_picker->theme_cache.sample_bg, Rect2(Point2(), size), true);

left_color = color;
left_color.a = 0;
right_color = color;
right_color.a = 1;

col.set(0, left_color);
col.set(1, right_color);
col.set(2, right_color);
col.set(3, left_color);
pos.set(0, Vector2(0, 0));
pos.set(1, Vector2(size.x, 0));
pos.set(2, Vector2(size.x, margin));
pos.set(3, Vector2(0, margin));

slider->draw_polygon(pos, col);
} else {
left_color = Color(
p_which == 0 ? 0 : color.r,
p_which == 1 ? 0 : color.g,
p_which == 2 ? 0 : color.b);
right_color = Color(
p_which == 0 ? 1 : color.r,
p_which == 1 ? 1 : color.g,
p_which == 2 ? 1 : color.b);
}
}

bool ColorModeRAW::apply_theme() const {
for (int i = 0; i < 4; i++) {
HSlider *slider = color_picker->get_slider(i);
slider->remove_theme_icon_override("grabber");
slider->remove_theme_icon_override("grabber_highlight");
slider->remove_theme_style_override("slider");
slider->remove_theme_constant_override("grabber_offset");
}
col.set(0, left_color);
col.set(1, right_color);
col.set(2, right_color);
col.set(3, left_color);
pos.set(0, Vector2());
pos.set(1, Vector2(size.x, 0));
pos.set(2, size);
pos.set(3, Vector2(0, size.y));

return true;
slider->draw_polygon(pos, col);
}

void ColorModeOKHSL::_value_changed() {
Expand Down Expand Up @@ -334,7 +339,6 @@ Color ColorModeOKHSL::get_color() const {
void ColorModeOKHSL::slider_draw(int p_which) {
HSlider *slider = color_picker->get_slider(p_which);
Size2 size = slider->get_size();
const real_t margin = 16 * color_picker->theme_cache.base_scale;

Vector<Vector2> pos;
Vector<Color> col;
Expand All @@ -359,18 +363,18 @@ void ColorModeOKHSL::slider_draw(int p_which) {
col.set(3, right_color);
col.set(4, middle_color);
col.set(5, left_color);
pos.set(0, Vector2(0, 0));
pos.set(0, Vector2());
pos.set(1, Vector2(size.x * 0.5, 0));
pos.set(2, Vector2(size.x, 0));
pos.set(3, Vector2(size.x, margin));
pos.set(4, Vector2(size.x * 0.5, margin));
pos.set(5, Vector2(0, margin));
pos.set(3, size);
pos.set(4, Vector2(size.x * 0.5, size.y));
pos.set(5, Vector2(0, size.y));
} else {
pos.resize(4);
col.resize(4);

if (p_which == ColorPicker::SLIDER_COUNT) {
slider->draw_texture_rect(color_picker->theme_cache.sample_bg, Rect2(Point2(0, 0), Size2(size.x, margin)), true);
slider->draw_texture_rect(color_picker->theme_cache.sample_bg, Rect2(Point2(), size), true);

left_color = color;
left_color.a = 0;
Expand All @@ -390,17 +394,17 @@ void ColorModeOKHSL::slider_draw(int p_which) {
col.set(1, right_color);
col.set(2, right_color);
col.set(3, left_color);
pos.set(0, Vector2(0, 0));
pos.set(0, Vector2());
pos.set(1, Vector2(size.x, 0));
pos.set(2, Vector2(size.x, margin));
pos.set(3, Vector2(0, margin));
pos.set(2, size);
pos.set(3, Vector2(0, size.y));
}

slider->draw_polygon(pos, col);

if (p_which == 0) { // H
Ref<Texture2D> hue = color_picker->theme_cache.color_okhsl_hue;
slider->draw_texture_rect(hue, Rect2(Vector2(), Vector2(size.x, margin)), false, Color::from_hsv(0, 0, color.get_ok_hsl_l() * 2.0, color.get_ok_hsl_s()));
slider->draw_texture_rect(hue, Rect2(Vector2(), size), false, Color::from_hsv(0, 0, color.get_ok_hsl_l() * 2.0, color.get_ok_hsl_s()));
return;
}
}
45 changes: 29 additions & 16 deletions scene/gui/color_mode.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,24 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

/**************************************************************************/
/* PIXEL ENGINE */
/* Copyright (c) 2024-present Pixel Engine contributors (see AUTHORS.md). */
/**************************************************************************/
/* NOTICE: */
/* This file contains modifications and additions specific to the Pixel */
/* Engine project. While these changes are licensed under the MIT license */
/* for compatibility, we request proper attribution if reused in any */
/* derivative works, including meta-forks. */
/**************************************************************************/

#ifndef COLOR_MODE_H
#define COLOR_MODE_H

#include "scene/gui/color_picker.h"
#include "core/string/ustring.h"

struct Color;
class ColorPicker;

class ColorMode {
public:
Expand All @@ -44,18 +58,17 @@ class ColorMode {
virtual float get_spinbox_arrow_step() const { return get_slider_step(); }
virtual String get_slider_label(int idx) const = 0;
virtual float get_slider_max(int idx) const = 0;
virtual bool can_allow_greater() const { return false; }
virtual float get_slider_value(int idx) const = 0;

virtual Color get_color() const = 0;

virtual void _value_changed() {}
virtual void _value_changed() {};

virtual void slider_draw(int p_which) = 0;
virtual bool apply_theme() const { return false; }
virtual ColorPicker::PickerShapeType get_shape_override() const { return ColorPicker::SHAPE_MAX; }

ColorMode(ColorPicker *p_color_picker);
virtual ~ColorMode() {}
virtual ~ColorMode() {};
};

class ColorModeHSV : public ColorMode {
Expand All @@ -79,48 +92,49 @@ class ColorModeHSV : public ColorMode {
virtual void slider_draw(int p_which) override;

ColorModeHSV(ColorPicker *p_color_picker) :
ColorMode(p_color_picker) {}
ColorMode(p_color_picker) {};
};

class ColorModeRGB : public ColorMode {
public:
String labels[3] = { "R", "G", "B" };
float slider_max[4] = { 255, 255, 255, 255 };

virtual String get_name() const override { return "RGB"; }

virtual float get_slider_step() const override { return 1; }
virtual String get_slider_label(int idx) const override;
virtual float get_slider_max(int idx) const override;
virtual bool can_allow_greater() const override { return true; }
virtual float get_slider_value(int idx) const override;

virtual Color get_color() const override;

virtual void slider_draw(int p_which) override;

ColorModeRGB(ColorPicker *p_color_picker) :
ColorMode(p_color_picker) {}
ColorMode(p_color_picker) {};
};

class ColorModeRAW : public ColorMode {
public:
String labels[3] = { "R", "G", "B" };
float slider_max[4] = { 100, 100, 100, 1 };
float slider_max[4] = { 1, 1, 1, 1 };

virtual String get_name() const override { return "RAW"; }

virtual float get_slider_step() const override { return 0.001; }
virtual float get_spinbox_arrow_step() const override { return 0.01; }
virtual float get_slider_step() const override { return 1.0 / 255.0; }
virtual String get_slider_label(int idx) const override;
virtual float get_slider_max(int idx) const override;
virtual bool can_allow_greater() const override { return true; }
virtual float get_slider_value(int idx) const override;

virtual Color get_color() const override;

virtual void slider_draw(int p_which) override;
virtual bool apply_theme() const override;

ColorModeRAW(ColorPicker *p_color_picker) :
ColorMode(p_color_picker) {}
ColorMode(p_color_picker) {};
};

class ColorModeOKHSL : public ColorMode {
Expand All @@ -130,7 +144,7 @@ class ColorModeOKHSL : public ColorMode {
float cached_hue = 0.0;
float cached_saturation = 0.0;

virtual String get_name() const override { return "OKHSL"; }
virtual String get_name() const override { return "HSL"; }

virtual float get_slider_step() const override { return 1.0; }
virtual String get_slider_label(int idx) const override;
Expand All @@ -142,12 +156,11 @@ class ColorModeOKHSL : public ColorMode {
virtual void _value_changed() override;

virtual void slider_draw(int p_which) override;
virtual ColorPicker::PickerShapeType get_shape_override() const override { return ColorPicker::SHAPE_OKHSL_CIRCLE; }

ColorModeOKHSL(ColorPicker *p_color_picker) :
ColorMode(p_color_picker) {}
ColorMode(p_color_picker) {};

~ColorModeOKHSL() {}
~ColorModeOKHSL() {};
};

#endif // COLOR_MODE_H
Loading

0 comments on commit 221143e

Please sign in to comment.