forked from vurtun/nuklear
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnuklear_color_picker.c
201 lines (181 loc) · 6.67 KB
/
nuklear_color_picker.c
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "nuklear.h"
#include "nuklear_internal.h"
/* ==============================================================
*
* COLOR PICKER
*
* ===============================================================*/
NK_LIB int
nk_color_picker_behavior(nk_flags *state,
const struct nk_rect *bounds, const struct nk_rect *matrix,
const struct nk_rect *hue_bar, const struct nk_rect *alpha_bar,
struct nk_colorf *color, const struct nk_input *in)
{
float hsva[4];
int value_changed = 0;
int hsv_changed = 0;
NK_ASSERT(state);
NK_ASSERT(matrix);
NK_ASSERT(hue_bar);
NK_ASSERT(color);
/* color matrix */
nk_colorf_hsva_fv(hsva, *color);
if (nk_button_behavior(state, *matrix, in, NK_BUTTON_REPEATER)) {
hsva[1] = NK_SATURATE((in->mouse.pos.x - matrix->x) / (matrix->w-1));
hsva[2] = 1.0f - NK_SATURATE((in->mouse.pos.y - matrix->y) / (matrix->h-1));
value_changed = hsv_changed = 1;
}
/* hue bar */
if (nk_button_behavior(state, *hue_bar, in, NK_BUTTON_REPEATER)) {
hsva[0] = NK_SATURATE((in->mouse.pos.y - hue_bar->y) / (hue_bar->h-1));
value_changed = hsv_changed = 1;
}
/* alpha bar */
if (alpha_bar) {
if (nk_button_behavior(state, *alpha_bar, in, NK_BUTTON_REPEATER)) {
hsva[3] = 1.0f - NK_SATURATE((in->mouse.pos.y - alpha_bar->y) / (alpha_bar->h-1));
value_changed = 1;
}
}
nk_widget_state_reset(state);
if (hsv_changed) {
*color = nk_hsva_colorfv(hsva);
*state = NK_WIDGET_STATE_ACTIVE;
}
if (value_changed) {
color->a = hsva[3];
*state = NK_WIDGET_STATE_ACTIVE;
}
/* set color picker widget state */
if (nk_input_is_mouse_hovering_rect(in, *bounds))
*state = NK_WIDGET_STATE_HOVERED;
if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, *bounds))
*state |= NK_WIDGET_STATE_ENTERED;
else if (nk_input_is_mouse_prev_hovering_rect(in, *bounds))
*state |= NK_WIDGET_STATE_LEFT;
return value_changed;
}
NK_LIB void
nk_draw_color_picker(struct nk_command_buffer *o, const struct nk_rect *matrix,
const struct nk_rect *hue_bar, const struct nk_rect *alpha_bar,
struct nk_colorf col)
{
NK_STORAGE const struct nk_color black = {0,0,0,255};
NK_STORAGE const struct nk_color white = {255, 255, 255, 255};
NK_STORAGE const struct nk_color black_trans = {0,0,0,0};
const float crosshair_size = 7.0f;
struct nk_color temp;
float hsva[4];
float line_y;
int i;
NK_ASSERT(o);
NK_ASSERT(matrix);
NK_ASSERT(hue_bar);
/* draw hue bar */
nk_colorf_hsva_fv(hsva, col);
for (i = 0; i < 6; ++i) {
NK_GLOBAL const struct nk_color hue_colors[] = {
{255, 0, 0, 255}, {255,255,0,255}, {0,255,0,255}, {0, 255,255,255},
{0,0,255,255}, {255, 0, 255, 255}, {255, 0, 0, 255}
};
nk_fill_rect_multi_color(o,
nk_rect(hue_bar->x, hue_bar->y + (float)i * (hue_bar->h/6.0f) + 0.5f,
hue_bar->w, (hue_bar->h/6.0f) + 0.5f), hue_colors[i], hue_colors[i],
hue_colors[i+1], hue_colors[i+1]);
}
line_y = (float)(int)(hue_bar->y + hsva[0] * matrix->h + 0.5f);
nk_stroke_line(o, hue_bar->x-1, line_y, hue_bar->x + hue_bar->w + 2,
line_y, 1, nk_rgb(255,255,255));
/* draw alpha bar */
if (alpha_bar) {
float alpha = NK_SATURATE(col.a);
line_y = (float)(int)(alpha_bar->y + (1.0f - alpha) * matrix->h + 0.5f);
nk_fill_rect_multi_color(o, *alpha_bar, white, white, black, black);
nk_stroke_line(o, alpha_bar->x-1, line_y, alpha_bar->x + alpha_bar->w + 2,
line_y, 1, nk_rgb(255,255,255));
}
/* draw color matrix */
temp = nk_hsv_f(hsva[0], 1.0f, 1.0f);
nk_fill_rect_multi_color(o, *matrix, white, temp, temp, white);
nk_fill_rect_multi_color(o, *matrix, black_trans, black_trans, black, black);
/* draw cross-hair */
{struct nk_vec2 p; float S = hsva[1]; float V = hsva[2];
p.x = (float)(int)(matrix->x + S * matrix->w);
p.y = (float)(int)(matrix->y + (1.0f - V) * matrix->h);
nk_stroke_line(o, p.x - crosshair_size, p.y, p.x-2, p.y, 1.0f, white);
nk_stroke_line(o, p.x + crosshair_size + 1, p.y, p.x+3, p.y, 1.0f, white);
nk_stroke_line(o, p.x, p.y + crosshair_size + 1, p.x, p.y+3, 1.0f, white);
nk_stroke_line(o, p.x, p.y - crosshair_size, p.x, p.y-2, 1.0f, white);}
}
NK_LIB int
nk_do_color_picker(nk_flags *state,
struct nk_command_buffer *out, struct nk_colorf *col,
enum nk_color_format fmt, struct nk_rect bounds,
struct nk_vec2 padding, const struct nk_input *in,
const struct nk_user_font *font)
{
int ret = 0;
struct nk_rect matrix;
struct nk_rect hue_bar;
struct nk_rect alpha_bar;
float bar_w;
NK_ASSERT(out);
NK_ASSERT(col);
NK_ASSERT(state);
NK_ASSERT(font);
if (!out || !col || !state || !font)
return ret;
bar_w = font->height;
bounds.x += padding.x;
bounds.y += padding.x;
bounds.w -= 2 * padding.x;
bounds.h -= 2 * padding.y;
matrix.x = bounds.x;
matrix.y = bounds.y;
matrix.h = bounds.h;
matrix.w = bounds.w - (3 * padding.x + 2 * bar_w);
hue_bar.w = bar_w;
hue_bar.y = bounds.y;
hue_bar.h = matrix.h;
hue_bar.x = matrix.x + matrix.w + padding.x;
alpha_bar.x = hue_bar.x + hue_bar.w + padding.x;
alpha_bar.y = bounds.y;
alpha_bar.w = bar_w;
alpha_bar.h = matrix.h;
ret = nk_color_picker_behavior(state, &bounds, &matrix, &hue_bar,
(fmt == NK_RGBA) ? &alpha_bar:0, col, in);
nk_draw_color_picker(out, &matrix, &hue_bar, (fmt == NK_RGBA) ? &alpha_bar:0, *col);
return ret;
}
NK_API int
nk_color_pick(struct nk_context * ctx, struct nk_colorf *color,
enum nk_color_format fmt)
{
struct nk_window *win;
struct nk_panel *layout;
const struct nk_style *config;
const struct nk_input *in;
enum nk_widget_layout_states state;
struct nk_rect bounds;
NK_ASSERT(ctx);
NK_ASSERT(color);
NK_ASSERT(ctx->current);
NK_ASSERT(ctx->current->layout);
if (!ctx || !ctx->current || !ctx->current->layout || !color)
return 0;
win = ctx->current;
config = &ctx->style;
layout = win->layout;
state = nk_widget(&bounds, ctx);
if (!state) return 0;
in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
return nk_do_color_picker(&ctx->last_widget_state, &win->buffer, color, fmt, bounds,
nk_vec2(0,0), in, config->font);
}
NK_API struct nk_colorf
nk_color_picker(struct nk_context *ctx, struct nk_colorf color,
enum nk_color_format fmt)
{
nk_color_pick(ctx, &color, fmt);
return color;
}