Nuklear
This is a minimal-state, immediate-mode graphical user interface toolkit written in ANSI C and licensed under public domain. It was designed as a simple embeddable user interface for application and does not have any dependencies, a default render backend or OS window/input handling but instead provides a highly modular, library-based approach, with simple input state for input and draw commands describing primitive shapes as output. So instead of providing a layered library that tries to abstract over a number of platform and render backends, it focuses only on the actual UI.
 
Loading...
Searching...
No Matches
nuklear_color_picker.c
1#include "nuklear.h"
2#include "nuklear_internal.h"
3
4/* ==============================================================
5 *
6 * COLOR PICKER
7 *
8 * ===============================================================*/
9NK_LIB nk_bool
10nk_color_picker_behavior(nk_flags *state,
11 const struct nk_rect *bounds, const struct nk_rect *matrix,
12 const struct nk_rect *hue_bar, const struct nk_rect *alpha_bar,
13 struct nk_colorf *color, const struct nk_input *in)
14{
15 float hsva[4];
16 nk_bool value_changed = 0;
17 nk_bool hsv_changed = 0;
18
19 NK_ASSERT(state);
20 NK_ASSERT(matrix);
21 NK_ASSERT(hue_bar);
22 NK_ASSERT(color);
23
24 /* color matrix */
25 nk_colorf_hsva_fv(hsva, *color);
26 if (nk_button_behavior(state, *matrix, in, NK_BUTTON_REPEATER)) {
27 hsva[1] = NK_SATURATE((in->mouse.pos.x - matrix->x) / (matrix->w-1));
28 hsva[2] = 1.0f - NK_SATURATE((in->mouse.pos.y - matrix->y) / (matrix->h-1));
29 value_changed = hsv_changed = 1;
30 }
31 /* hue bar */
32 if (nk_button_behavior(state, *hue_bar, in, NK_BUTTON_REPEATER)) {
33 hsva[0] = NK_SATURATE((in->mouse.pos.y - hue_bar->y) / (hue_bar->h-1));
34 value_changed = hsv_changed = 1;
35 }
36 /* alpha bar */
37 if (alpha_bar) {
38 if (nk_button_behavior(state, *alpha_bar, in, NK_BUTTON_REPEATER)) {
39 hsva[3] = 1.0f - NK_SATURATE((in->mouse.pos.y - alpha_bar->y) / (alpha_bar->h-1));
40 value_changed = 1;
41 }
42 }
43 nk_widget_state_reset(state);
44 if (hsv_changed) {
45 *color = nk_hsva_colorfv(hsva);
47 }
48 if (value_changed) {
49 color->a = hsva[3];
51 }
52 /* set color picker widget state */
53 if (nk_input_is_mouse_hovering_rect(in, *bounds))
55 if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, *bounds))
57 else if (nk_input_is_mouse_prev_hovering_rect(in, *bounds))
58 *state |= NK_WIDGET_STATE_LEFT;
59 return value_changed;
60}
61NK_LIB void
62nk_draw_color_picker(struct nk_command_buffer *o, const struct nk_rect *matrix,
63 const struct nk_rect *hue_bar, const struct nk_rect *alpha_bar,
64 struct nk_colorf col)
65{
66 NK_STORAGE const struct nk_color black = {0,0,0,255};
67 NK_STORAGE const struct nk_color white = {255, 255, 255, 255};
68 NK_STORAGE const struct nk_color black_trans = {0,0,0,0};
69
70 const float crosshair_size = 7.0f;
71 struct nk_color temp;
72 float hsva[4];
73 float line_y;
74 int i;
75
76 NK_ASSERT(o);
77 NK_ASSERT(matrix);
78 NK_ASSERT(hue_bar);
79
80 /* draw hue bar */
81 nk_colorf_hsva_fv(hsva, col);
82 for (i = 0; i < 6; ++i) {
83 NK_GLOBAL const struct nk_color hue_colors[] = {
84 {255, 0, 0, 255}, {255,255,0,255}, {0,255,0,255}, {0, 255,255,255},
85 {0,0,255,255}, {255, 0, 255, 255}, {255, 0, 0, 255}
86 };
87 nk_fill_rect_multi_color(o,
88 nk_rect(hue_bar->x, hue_bar->y + (float)i * (hue_bar->h/6.0f) + 0.5f,
89 hue_bar->w, (hue_bar->h/6.0f) + 0.5f), hue_colors[i], hue_colors[i],
90 hue_colors[i+1], hue_colors[i+1]);
91 }
92 line_y = (float)(int)(hue_bar->y + hsva[0] * matrix->h + 0.5f);
93 nk_stroke_line(o, hue_bar->x-1, line_y, hue_bar->x + hue_bar->w + 2,
94 line_y, 1, nk_rgb(255,255,255));
95
96 /* draw alpha bar */
97 if (alpha_bar) {
98 float alpha = NK_SATURATE(col.a);
99 line_y = (float)(int)(alpha_bar->y + (1.0f - alpha) * matrix->h + 0.5f);
100
101 nk_fill_rect_multi_color(o, *alpha_bar, white, white, black, black);
102 nk_stroke_line(o, alpha_bar->x-1, line_y, alpha_bar->x + alpha_bar->w + 2,
103 line_y, 1, nk_rgb(255,255,255));
104 }
105
106 /* draw color matrix */
107 temp = nk_hsv_f(hsva[0], 1.0f, 1.0f);
108 nk_fill_rect_multi_color(o, *matrix, white, temp, temp, white);
109 nk_fill_rect_multi_color(o, *matrix, black_trans, black_trans, black, black);
110
111 /* draw cross-hair */
112 {struct nk_vec2 p; float S = hsva[1]; float V = hsva[2];
113 p.x = (float)(int)(matrix->x + S * matrix->w);
114 p.y = (float)(int)(matrix->y + (1.0f - V) * matrix->h);
115 nk_stroke_line(o, p.x - crosshair_size, p.y, p.x-2, p.y, 1.0f, white);
116 nk_stroke_line(o, p.x + crosshair_size + 1, p.y, p.x+3, p.y, 1.0f, white);
117 nk_stroke_line(o, p.x, p.y + crosshair_size + 1, p.x, p.y+3, 1.0f, white);
118 nk_stroke_line(o, p.x, p.y - crosshair_size, p.x, p.y-2, 1.0f, white);}
119}
120NK_LIB nk_bool
121nk_do_color_picker(nk_flags *state,
122 struct nk_command_buffer *out, struct nk_colorf *col,
123 enum nk_color_format fmt, struct nk_rect bounds,
124 struct nk_vec2 padding, const struct nk_input *in,
125 const struct nk_user_font *font)
126{
127 int ret = 0;
128 struct nk_rect matrix;
129 struct nk_rect hue_bar;
130 struct nk_rect alpha_bar;
131 float bar_w;
132
133 NK_ASSERT(out);
134 NK_ASSERT(col);
135 NK_ASSERT(state);
136 NK_ASSERT(font);
137 if (!out || !col || !state || !font)
138 return ret;
139
140 bar_w = font->height;
141 bounds.x += padding.x;
142 bounds.y += padding.x;
143 bounds.w -= 2 * padding.x;
144 bounds.h -= 2 * padding.y;
145
146 matrix.x = bounds.x;
147 matrix.y = bounds.y;
148 matrix.h = bounds.h;
149 matrix.w = bounds.w - (3 * padding.x + 2 * bar_w);
150
151 hue_bar.w = bar_w;
152 hue_bar.y = bounds.y;
153 hue_bar.h = matrix.h;
154 hue_bar.x = matrix.x + matrix.w + padding.x;
155
156 alpha_bar.x = hue_bar.x + hue_bar.w + padding.x;
157 alpha_bar.y = bounds.y;
158 alpha_bar.w = bar_w;
159 alpha_bar.h = matrix.h;
160
161 ret = nk_color_picker_behavior(state, &bounds, &matrix, &hue_bar,
162 (fmt == NK_RGBA) ? &alpha_bar:0, col, in);
163 nk_draw_color_picker(out, &matrix, &hue_bar, (fmt == NK_RGBA) ? &alpha_bar:0, *col);
164 return ret;
165}
166NK_API nk_bool
167nk_color_pick(struct nk_context * ctx, struct nk_colorf *color,
168 enum nk_color_format fmt)
169{
170 struct nk_window *win;
171 struct nk_panel *layout;
172 const struct nk_style *config;
173 const struct nk_input *in;
174
175 enum nk_widget_layout_states state;
176 struct nk_rect bounds;
177
178 NK_ASSERT(ctx);
179 NK_ASSERT(color);
180 NK_ASSERT(ctx->current);
181 NK_ASSERT(ctx->current->layout);
182 if (!ctx || !ctx->current || !ctx->current->layout || !color)
183 return 0;
184
185 win = ctx->current;
186 config = &ctx->style;
187 layout = win->layout;
188 state = nk_widget(&bounds, ctx);
189 if (!state) return 0;
190 in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
191 return nk_do_color_picker(&ctx->last_widget_state, &win->buffer, color, fmt, bounds,
192 nk_vec2(0,0), in, config->font);
193}
194NK_API struct nk_colorf
195nk_color_picker(struct nk_context *ctx, struct nk_colorf color,
196 enum nk_color_format fmt)
197{
198 nk_color_pick(ctx, &color, fmt);
199 return color;
200}
201
main API and documentation file
@ NK_WINDOW_ROM
sets window widgets into a read only mode and does not allow input changes
Definition nuklear.h:5492
NK_API void nk_stroke_line(struct nk_command_buffer *b, float x0, float y0, float x1, float y1, float line_thickness, struct nk_color)
shape outlines
@ NK_WIDGET_STATE_LEFT
!< widget is currently activated
Definition nuklear.h:3093
@ NK_WIDGET_STATE_ENTERED
!< widget is neither active nor hovered
Definition nuklear.h:3090
@ NK_WIDGET_STATE_HOVER
!< widget has been hovered on the current frame
Definition nuklear.h:3091
@ NK_WIDGET_STATE_ACTIVE
!< widget is being hovered
Definition nuklear.h:3095
@ NK_WIDGET_STATE_HOVERED
!< widget is from this frame on not hovered anymore
Definition nuklear.h:3094
nk_widget_layout_states
Definition nuklear.h:3081
@ NK_WIDGET_DISABLED
The widget is manually disabled and acts like NK_WIDGET_ROM.
Definition nuklear.h:3085
@ NK_WIDGET_ROM
The widget is partially visible and cannot be updated.
Definition nuklear.h:3084
float height
!< user provided font handle
Definition nuklear.h:4008