Skip to content
Robert J. Lemmens edited this page Jan 5, 2022 · 4 revisions

Color Picker

colorpicker1

Description

The Color Picker is useful for selecting colors and all other coloring needs. You can create one with nk_color_picker(...):

struct nk_colorf my_color = {0.8f, 0.3f, 0.2f, 1.0f};
my_color = nk_color_picker(ctx, my_color, NK_RGBA);

It takes an nk_colorf and nk_color_format as parameter (the display color) and returns an nk_colorf with the newly selected color.

Examples

Heres an example of a window with a color picker that changes the color of some text:

static void scratchpad(struct nk_context *ctx, struct media *media) {
  static struct nk_colorf my_color = {0.8f, 0.3f, 0.2f, 1.0f};
  static const char my_text[] = "What a nice color! Or is it colour...?";
  nk_style_set_font(ctx, &media->font_20->handle);
  nk_begin(ctx, "Nuklear Color Picker example", nk_rect(50,50, 255, 340), NK_WINDOW_TITLE | NK_WINDOW_MOVABLE);
    nk_layout_row_dynamic(ctx, 200, 1);
    my_color = nk_color_picker(ctx, my_color, NK_RGBA);           /// Our color picker, returns the newly selected color which we can then use elsewhere
    nk_layout_row_dynamic(ctx, 20, 1);
    nk_text_colored(ctx, my_text, strlen(my_text), NK_TEXT_LEFT, nk_rgb_cf(my_color));   /// nk_text_colored takes an rgb color instead of rgba so we have to do some magic (nk_rgb_cf)
  nk_end(ctx);
}

And viola!

Color Picker

Note

The colorpicker takes an nk_colorf (0-1, float) for coloring. Some components, like the text in the example above, need an nk_color which is in a different format (0-255, byte). nk_rgb_cf(...) will take an nk_colorf and return a corresponding nk_color for you.

Clone this wiki locally