Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 418033479d5d5ad3794ad3a05fa43527d0fb2196 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
   ui/colorpicker.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#include "ui/bitmap.h"
#include "ui/colorpicker.h"
#include "ui/slider.h"
#include "ui/label.h"
#include "ui/ui.h"

#include "math/functions.h"

namespace ui 
{
	
ColorPicker::ColorPicker(Widget *parent) : Widget(parent)
{
	set_label("colorpicker");
	
	_label_red = new Label(this, "R");
	_label_red->set_alignment(AlignLeft | AlignVCenter);
	_label_red->set_border(false);
	_label_red->set_background(false);
	_slider_red = new Slider(this, 0.0f, 255.0f);
	
	_label_green = new Label(this, "G");
	_label_green->set_alignment(AlignLeft | AlignVCenter);
	_label_green->set_border(false);
	_label_green->set_background(false);
	_slider_green = new Slider(this, 0.0f, 255.0f);
	
	_label_blue = new Label(this, "B");
	_label_blue->set_alignment(AlignLeft | AlignVCenter);
	_label_blue->set_border(false);
	_label_blue->set_background(false);
	_slider_blue = new Slider(this, 0.0f, 255.0f);
	
	_color_preview = new Bitmap(this);
	_color_preview->set_label("preview");
	_color_preview->set_border(true);

	color_changed();
}

ColorPicker::~ColorPicker()
{
}

void ColorPicker::set_color(const math::Color & color)
{
	_color.assign(color);
	color_changed();
}
	
void ColorPicker::set_color(const float r, const float g, const float b)
{
	_color.assign(r, g, b);
	color_changed();
}

void ColorPicker::color_changed()
{
	_slider_red->set_value(_color.red() * 255.0f);
	_slider_green->set_value(_color.green() * 255.0f);
	_slider_blue->set_value(_color.blue() * 255.0f);
	_color_preview->set_color(_color);
}

void ColorPicker::resize()
{
	const float margin = UI::margin;
	const float w = font()->width() * 3.0f;
	const float h = (height() - margin * 4.0f) / 3.0f;
	
	const float r = math::min(width(), height()) - 2.0f * margin;
	
	// resize color preview
	_color_preview->set_size(r,r);
	_color_preview->set_location(width() - r - margin, height() - r - margin);
	
	// resize red channel label
	_label_red->set_size(w, h);
	_label_red->set_location(margin, margin);
	// resize red channel slider
	_slider_red->set_location(_label_red->right(), _label_red->top());
	_slider_red->set_size(_color_preview->left() - _label_red->right() - 2.0f * margin, h);
	
	_label_green->set_size(w, h);
	_label_green->set_location(margin, 2.0f * margin + h);
	// resize green channel slider
	_slider_green->set_location(_label_green->right(), _label_green->top());
	_slider_green->set_size(_color_preview->left() - _label_green->right() - 2.0f * margin, h);
	
	_label_blue->set_size(w, h);
	_label_blue->set_location(margin, 3.0f * margin + 2.0f * h);
	// resize blue channel slider
	_slider_blue->set_location(_label_blue->right(), _label_blue->top());
	_slider_blue->set_size(_color_preview->left() - _label_blue->right() - 2.0f * margin, h);
}

bool ColorPicker::on_emit(Widget *sender, const Event event, void *data)
{
	if (event == ui::Widget::EventSliderChanged)
	{
		if (sender == _slider_red)
		{
			_color[0] = _slider_red->value() / 255.0f;
			_color_preview->set_color(_color);
			return true;
		}
		else if (sender == _slider_green)
		{
			_color[1] = _slider_green->value() / 255.0f;
			_color_preview->set_color(_color);
			return true;
		}
		else if (sender == _slider_blue)
		{
			_color[2] = _slider_blue->value() / 255.0f;
			_color_preview->set_color(_color);
			return true;
		}	
	}
	
	// unhandled event
	return false;
}

}