Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_gui_color.c File Reference

A colour picker dialog for N_GUI, and a swatch to show a colour. More...

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <allegro5/allegro_primitives.h>
#include "nilorea/n_common.h"
#include "nilorea/n_log.h"
#include "nilorea/n_gui_color.h"
Include dependency graph for n_gui_color.c:

Go to the source code of this file.

Data Structures

struct  N_GUI_COLOR_PICKER
 a colour picker: the ids of its widgets and the colour being edited More...

Macros

#define N_GUI_COLOR_BTN_W   90.0f
 width of the Close button at scale 1
#define N_GUI_COLOR_CHECK   6.0f
 side of one checkerboard square behind a swatch, in pixels.
#define N_GUI_COLOR_LABEL   18.0f
 height of a label at scale 1
#define N_GUI_COLOR_PAD   10.0f
 padding at scale 1
#define N_GUI_COLOR_ROW   24.0f
 height of a slider or button row at scale 1
#define N_GUI_COLOR_SWATCH   48.0f
 height of the large swatch at scale 1
#define N_GUI_COLOR_W   300.0f
 window width at scale 1: wide enough that a slider moved by one step in 255 moves by at least a pixel

Functions

static float _n_gui_color_clamp (float v)
 helper: clamp a channel to 0..1
static void _n_gui_color_draw_swatch (N_GUI_WIDGET *widget, float x, float y, ALLEGRO_FONT *font, void *user_data)
 custom draw callback of the large swatch
static void _n_gui_color_on_close_button (int widget_id, void *user_data)
 button callback: Close
static void _n_gui_color_on_close_window (int window_id, void *user_data)
 title bar close: the same as the Close button, so the callback is forgotten either way
static void _n_gui_color_on_slider (int widget_id, double value, void *user_data)
 slider callback: one channel moved
static void _n_gui_color_set_label (N_GUI_COLOR_PICKER *picker, int channel)
 helper: write one channel's label, its name and its value out of 255
void n_gui_color_picker_close (N_GUI_COLOR_PICKER *picker)
 Close the picker.
void n_gui_color_picker_free (N_GUI_COLOR_PICKER **picker)
 Close the picker's window and free the handle.
int n_gui_color_picker_get (const N_GUI_COLOR_PICKER *picker, float rgba[4])
 The colour the picker holds now.
int n_gui_color_picker_is_open (const N_GUI_COLOR_PICKER *picker)
 Whether the picker is open.
N_GUI_COLOR_PICKER * n_gui_color_picker_new (N_GUI_CTX *ctx, const char *title, float scale)
 Build a colour picker window in ctx, closed.
void n_gui_color_picker_open (N_GUI_COLOR_PICKER *picker, const char *what, const float rgba[4], N_GUI_COLOR_FN on_change, void *user_data)
 Open the picker on a colour, centred on the display.
int n_gui_color_picker_slider (const N_GUI_COLOR_PICKER *picker, int channel)
 The slider for one channel, for a caller or a test that drives it.
int n_gui_color_picker_window (const N_GUI_COLOR_PICKER *picker)
 The picker's window id, for a caller that walks its windows or sets their flags.
void n_gui_color_to_hex (const float rgba[4], char *out, size_t len)
 Write a colour as #RRGGBBAA, each channel clamped to 0..1 and rounded to the nearest of 255 steps.
void n_gui_draw_color_swatch (float x, float y, float w, float h, const float rgba[4])
 Draw a colour as a swatch, over a checkerboard so its alpha shows, with a thin dark border.
ALLEGRO_MOUSE_CURSOR * n_gui_eyedropper_cursor (float scale)
 Make a pipette mouse cursor, for a tool that picks a colour from an image.

Variables

static const char *const _n_gui_color_channels [4] = {"Red", "Green", "Blue", "Alpha"}
 the four channels, in the order of the sliders

Detailed Description

A colour picker dialog for N_GUI, and a swatch to show a colour.

Author
Castagnier Mickael
Version
1.0
Date
24/09/2026

Definition in file n_gui_color.c.


Data Structure Documentation

◆ N_GUI_COLOR_PICKER

struct N_GUI_COLOR_PICKER

a colour picker: the ids of its widgets and the colour being edited

a colour picker dialog.

Opaque: make it with n_gui_color_picker_new(), release it with n_gui_color_picker_free().

Examples
ex_gui_color_picker.c.

Definition at line 61 of file n_gui_color.c.

Collaboration diagram for N_GUI_COLOR_PICKER:
Data Fields
int btn_close the Close button
N_GUI_CTX * ctx the context the widgets live in
int lbl[4] name and value over each slider
int lbl_what what the colour is for
N_GUI_COLOR_FN on_change whom to tell
float rgba[4] the colour being edited
int sl[4] one slider per channel, 0 to 255
int swatch the large swatch
int syncing non-zero while the sliders are set from code, so their callbacks are not taken for the user's
void * user_data handed back to on_change
int window the window

Macro Definition Documentation

◆ N_GUI_COLOR_BTN_W

#define N_GUI_COLOR_BTN_W   90.0f

width of the Close button at scale 1

Definition at line 52 of file n_gui_color.c.

Referenced by n_gui_color_picker_new().

◆ N_GUI_COLOR_CHECK

#define N_GUI_COLOR_CHECK   6.0f

side of one checkerboard square behind a swatch, in pixels.

Not scaled: it only has to show that something is see-through.

Definition at line 55 of file n_gui_color.c.

Referenced by n_gui_draw_color_swatch().

◆ N_GUI_COLOR_LABEL

#define N_GUI_COLOR_LABEL   18.0f

height of a label at scale 1

Definition at line 46 of file n_gui_color.c.

Referenced by n_gui_color_picker_new().

◆ N_GUI_COLOR_PAD

#define N_GUI_COLOR_PAD   10.0f

padding at scale 1

Definition at line 50 of file n_gui_color.c.

Referenced by n_gui_color_picker_new().

◆ N_GUI_COLOR_ROW

#define N_GUI_COLOR_ROW   24.0f

height of a slider or button row at scale 1

Definition at line 44 of file n_gui_color.c.

Referenced by n_gui_color_picker_new().

◆ N_GUI_COLOR_SWATCH

#define N_GUI_COLOR_SWATCH   48.0f

height of the large swatch at scale 1

Definition at line 48 of file n_gui_color.c.

Referenced by n_gui_color_picker_new().

◆ N_GUI_COLOR_W

#define N_GUI_COLOR_W   300.0f

window width at scale 1: wide enough that a slider moved by one step in 255 moves by at least a pixel

Definition at line 42 of file n_gui_color.c.

Referenced by n_gui_color_picker_new().

Function Documentation

◆ _n_gui_color_clamp()

float _n_gui_color_clamp ( float v)
static

helper: clamp a channel to 0..1

Definition at line 88 of file n_gui_color.c.

Referenced by n_gui_color_picker_open(), n_gui_color_to_hex(), and n_gui_draw_color_swatch().

Here is the caller graph for this function:

◆ _n_gui_color_draw_swatch()

void _n_gui_color_draw_swatch ( N_GUI_WIDGET * widget,
float x,
float y,
ALLEGRO_FONT * font,
void * user_data )
static

custom draw callback of the large swatch

Definition at line 188 of file n_gui_color.c.

References font, N_GUI_WIDGET::h, n_gui_draw_color_swatch(), N_GUI_COLOR_PICKER::rgba, and N_GUI_WIDGET::w.

Referenced by n_gui_color_picker_new().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _n_gui_color_on_close_button()

void _n_gui_color_on_close_button ( int widget_id,
void * user_data )
static

button callback: Close

Definition at line 222 of file n_gui_color.c.

References n_gui_color_picker_close().

Referenced by n_gui_color_picker_new().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _n_gui_color_on_close_window()

void _n_gui_color_on_close_window ( int window_id,
void * user_data )
static

title bar close: the same as the Close button, so the callback is forgotten either way

Definition at line 229 of file n_gui_color.c.

References n_gui_color_picker_close().

Referenced by n_gui_color_picker_new().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _n_gui_color_on_slider()

void _n_gui_color_on_slider ( int widget_id,
double value,
void * user_data )
static

slider callback: one channel moved

Definition at line 205 of file n_gui_color.c.

References _n_gui_color_set_label(), N_GUI_COLOR_PICKER::ctx, n_gui_mark_dirty(), N_GUI_COLOR_PICKER::on_change, N_GUI_COLOR_PICKER::rgba, N_GUI_COLOR_PICKER::sl, N_GUI_COLOR_PICKER::syncing, and N_GUI_COLOR_PICKER::user_data.

Referenced by n_gui_color_picker_new().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _n_gui_color_set_label()

void _n_gui_color_set_label ( N_GUI_COLOR_PICKER * picker,
int channel )
static

helper: write one channel's label, its name and its value out of 255

Definition at line 197 of file n_gui_color.c.

References _n_gui_color_channels, N_GUI_COLOR_PICKER::ctx, N_GUI_COLOR_PICKER::lbl, n_gui_label_set_text(), and N_GUI_COLOR_PICKER::rgba.

Referenced by _n_gui_color_on_slider(), and n_gui_color_picker_open().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ _n_gui_color_channels

const char* const _n_gui_color_channels[4] = {"Red", "Green", "Blue", "Alpha"}
static

the four channels, in the order of the sliders

Definition at line 58 of file n_gui_color.c.

Referenced by _n_gui_color_set_label(), and n_gui_color_picker_new().