From 4ed600b6216852d4324ef80e03461ceecda35832 Mon Sep 17 00:00:00 2001 From: Maticzpl <27724912+Maticzpl@users.noreply.github.com> Date: Thu, 29 Jul 2021 17:23:39 +0200 Subject: [PATCH] All HSV sliders in color picker (#796) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tamás Bálint Misius --- src/gui/colourpicker/ColourPickerActivity.cpp | 129 ++++++++++-------- src/gui/colourpicker/ColourPickerActivity.h | 7 +- 2 files changed, 76 insertions(+), 60 deletions(-) diff --git a/src/gui/colourpicker/ColourPickerActivity.cpp b/src/gui/colourpicker/ColourPickerActivity.cpp index 4a8c370c0..19f105159 100644 --- a/src/gui/colourpicker/ColourPickerActivity.cpp +++ b/src/gui/colourpicker/ColourPickerActivity.cpp @@ -1,6 +1,7 @@ #include "ColourPickerActivity.h" #include "gui/interface/Textbox.h" +#include "gui/interface/Slider.h" #include "gui/interface/Button.h" #include "gui/interface/Label.h" #include "gui/interface/Keys.h" @@ -11,12 +12,11 @@ #include "Misc.h" ColourPickerActivity::ColourPickerActivity(ui::Colour initialColour, OnPicked onPicked_) : - WindowActivity(ui::Point(-1, -1), ui::Point(266, 175)), + WindowActivity(ui::Point(-1, -1), ui::Point(266, 215)), currentHue(0), currentSaturation(0), currentValue(0), mouseDown(false), - valueMouseDown(false), onPicked(onPicked_) { auto colourChange = [this] { @@ -37,8 +37,34 @@ ColourPickerActivity::ColourPickerActivity(ui::Colour initialColour, OnPicked on RGB_to_HSV(r, g, b, ¤tHue, ¤tSaturation, ¤tValue); currentAlpha = alpha; UpdateTextboxes(r, g, b, alpha); + UpdateSliders(); }; + auto colourChangeSlider = [this] { + int r, g, b; + currentHue = hSlider->GetValue(); + currentSaturation = sSlider->GetValue(); + currentValue = vSlider->GetValue(); + + HSV_to_RGB(currentHue, currentSaturation, currentValue, &r, &g, &b); + UpdateTextboxes(r, g, b, currentAlpha); + UpdateSliders(); + }; + + + hSlider = new ui::Slider(ui::Point(0,134), ui::Point(Size.X,17), 359); + hSlider->SetActionCallback({ colourChangeSlider }); + AddComponent(hSlider); + + sSlider = new ui::Slider(ui::Point(0, 134 + 17), ui::Point(Size.X, 17), 255); + sSlider->SetActionCallback({ colourChangeSlider }); + AddComponent(sSlider); + + vSlider = new ui::Slider(ui::Point(0, 134 + 34), ui::Point(Size.X, 17), 255); + vSlider->SetActionCallback({ colourChangeSlider }); + AddComponent(vSlider); + + rValue = new ui::Textbox(ui::Point(5, Size.Y-23), ui::Point(30, 17), "255"); rValue->SetActionCallback({ colourChange }); rValue->SetLimit(3); @@ -83,6 +109,7 @@ ColourPickerActivity::ColourPickerActivity(ui::Colour initialColour, OnPicked on RGB_to_HSV(initialColour.Red, initialColour.Green, initialColour.Blue, ¤tHue, ¤tSaturation, ¤tValue); currentAlpha = initialColour.Alpha; UpdateTextboxes(initialColour.Red, initialColour.Green, initialColour.Blue, initialColour.Alpha); + UpdateSliders(); } void ColourPickerActivity::UpdateTextboxes(int r, int g, int b, int a) @@ -93,6 +120,27 @@ void ColourPickerActivity::UpdateTextboxes(int r, int g, int b, int a) aValue->SetText(String::Build(a)); hexValue->SetText(String::Build(Format::Hex(), Format::Uppercase(), Format::Width(2), a, r, g, b)); } + +void ColourPickerActivity::UpdateSliders() +{ + hSlider->SetValue(currentHue); + sSlider->SetValue(currentSaturation); + vSlider->SetValue(currentValue); + + int r, g, b; + + //Value gradient + HSV_to_RGB(currentHue, currentSaturation, 255, &r, &g, &b); + vSlider->SetColour(ui::Colour(0, 0, 0), ui::Colour(r, g, b)); + + //Saturation gradient + if (currentValue != 0) + { + HSV_to_RGB(currentHue, 255, currentValue, &r, &g, &b); + sSlider->SetColour(ui::Colour(currentValue, currentValue, currentValue), ui::Colour(r, g, b)); + } +} + void ColourPickerActivity::OnTryExit(ExitMethod method) { Exit(); @@ -118,24 +166,12 @@ void ColourPickerActivity::OnMouseMove(int x, int y, int dx, int dy) currentHue = 0; } - if(valueMouseDown) - { - x -= Position.X+5; - //y -= Position.Y+5; - - currentValue = x; - - if(currentValue > 255) - currentValue = 255; - if(currentValue < 0) - currentValue = 0; - } - - if(mouseDown || valueMouseDown) + if(mouseDown) { int cr, cg, cb; HSV_to_RGB(currentHue, currentSaturation, currentValue, &cr, &cg, &cb); UpdateTextboxes(cr, cg, cb, currentAlpha); + UpdateSliders(); } } @@ -159,32 +195,23 @@ void ColourPickerActivity::OnMouseDown(int x, int y, unsigned button) currentHue = 0; } - if(x >= 0 && x < 256 && y >= 132 && y <= 142) - { - valueMouseDown = true; - currentValue = x; - - if(currentValue > 255) - currentValue = 255; - if(currentValue < 0) - currentValue = 0; - } - - if(mouseDown || valueMouseDown) + if(mouseDown) { int cr, cg, cb; HSV_to_RGB(currentHue, currentSaturation, currentValue, &cr, &cg, &cb); UpdateTextboxes(cr, cg, cb, currentAlpha); + UpdateSliders(); } } void ColourPickerActivity::OnMouseUp(int x, int y, unsigned button) { - if(mouseDown || valueMouseDown) + if(mouseDown) { int cr, cg, cb; HSV_to_RGB(currentHue, currentSaturation, currentValue, &cr, &cg, &cb); UpdateTextboxes(cr, cg, cb, currentAlpha); + UpdateSliders(); } if(mouseDown) @@ -205,21 +232,6 @@ void ColourPickerActivity::OnMouseUp(int x, int y, unsigned button) if(currentHue < 0) currentHue = 0; } - - if(valueMouseDown) - { - valueMouseDown = false; - - x -= Position.X+5; - //y -= Position.Y+5; - - currentValue = x; - - if(currentValue > 255) - currentValue = 255; - if(currentValue < 0) - currentValue = 0; - } } void ColourPickerActivity::OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt) @@ -248,8 +260,6 @@ void ColourPickerActivity::OnDraw() g->drawrect(Position.X+4, Position.Y+4, 258, 130, 180, 180, 180, 255); - g->drawrect(Position.X+4, Position.Y+4+4+128, 258, 12, 180, 180, 180, 255); - int offsetX = Position.X+5; int offsetY = Position.Y+5; @@ -273,17 +283,22 @@ void ColourPickerActivity::OnDraw() } } - //draw brightness bar - for(int value = 0; value <= 255; value++) - for(int i = 0; i < 10; i++) + //Draw hue bar gradient + auto gradientWidth = hSlider->Size.X - 10; + for (int rx = 0; rx < gradientWidth; rx++) + { + int red, green, blue; + int hue = rx * 360 / gradientWidth; + HSV_to_RGB(hue, currentSaturation, currentValue, &red, &green, &blue); + for (int ry = 0; ry < (hSlider->Size.Y / 2) - 1; ry++) { - int cr = 0; - int cg = 0; - int cb = 0; - HSV_to_RGB(currentHue, currentSaturation, value, &cr, &cg, &cb); - - g->blendpixel(value+offsetX, i+offsetY+127+5, cr, cg, cb, currentAlpha); + g->blendpixel( + rx + offsetX + hSlider->Position.X, + ry + offsetY + hSlider->Position.Y, + red, green, blue, currentAlpha + ); } + } //draw color square pointer int currentHueX = clamp_flt(float(currentHue), 0, 359); @@ -293,8 +308,4 @@ void ColourPickerActivity::OnDraw() g->xor_line(offsetX+currentHueX-5, offsetY+currentSaturationY, offsetX+currentHueX-1, offsetY+currentSaturationY); g->xor_line(offsetX+currentHueX+1, offsetY+currentSaturationY, offsetX+currentHueX+5, offsetY+currentSaturationY); - //draw brightness bar pointer - int currentValueX = int(restrict_flt(float(currentValue), 0, 254)); - g->xor_line(offsetX+currentValueX, offsetY+4+128, offsetX+currentValueX, offsetY+13+128); - g->xor_line(offsetX+currentValueX+1, offsetY+4+128, offsetX+currentValueX+1, offsetY+13+128); } diff --git a/src/gui/colourpicker/ColourPickerActivity.h b/src/gui/colourpicker/ColourPickerActivity.h index 6110250e6..421bb38e2 100644 --- a/src/gui/colourpicker/ColourPickerActivity.h +++ b/src/gui/colourpicker/ColourPickerActivity.h @@ -10,6 +10,7 @@ namespace ui { class Textbox; class Label; + class Slider; } class ColourPickerActivity : public WindowActivity @@ -22,7 +23,10 @@ class ColourPickerActivity : public WindowActivity int currentAlpha; bool mouseDown; - bool valueMouseDown; + + ui::Slider* hSlider; + ui::Slider* sSlider; + ui::Slider* vSlider; ui::Textbox * rValue; ui::Textbox * gValue; @@ -33,6 +37,7 @@ class ColourPickerActivity : public WindowActivity OnPicked onPicked; void UpdateTextboxes(int r, int g, int b, int a); + void UpdateSliders(); public: ColourPickerActivity(ui::Colour initialColour, OnPicked onPicked = nullptr); virtual ~ColourPickerActivity() = default;