6 n__gui__color_8h
gullradriel edited this page 2026-10-09 00:55:18 +02:00

include/nilorea/n_gui_color.h

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

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

The picker is a popup window built from ordinary N_GUI widgets: a line saying what the colour is for, a large swatch over a checkerboard, four sliders 0 to 255 for red, green, blue and alpha, and a Close button. It calls back on every change rather than once on close, because whatever the colour is for should follow it as the slider moves; there is therefore nothing to cancel.

One picker serves any number of colours: opening it again, on another colour, replaces what it edits and whom it tells.

A colour is four floats, red, green, blue and alpha, each 0 to 1, display encoded, straight (not premultiplied) alpha. The picker keeps them in steps of 1/255, so the number on a label is the number that is kept.

n_gui_draw_color_swatch() is usable on its own, from a custom widget or anywhere else, to show a colour beside the button that opens the picker.

Source code

/*
 * Nilorea Library
 * Copyright (C) 2005-2026 Castagnier Mickael
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 */


#ifndef __NILOREA_GUI_COLOR__
#define __NILOREA_GUI_COLOR__

/* Allegro only: the picker is N_GUI widgets and the swatch is drawn with
 * allegro_primitives. */
#ifdef HAVE_ALLEGRO

#ifdef __cplusplus
extern "C" {
#endif


#include <stddef.h>

#include "nilorea/n_gui.h"

typedef struct N_GUI_COLOR_PICKER N_GUI_COLOR_PICKER;

typedef void (*N_GUI_COLOR_FN)(const float rgba[4], void* user_data);

N_GUI_COLOR_PICKER* n_gui_color_picker_new(N_GUI_CTX* ctx, const char* title, float scale);

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);

void n_gui_color_picker_close(N_GUI_COLOR_PICKER* picker);

int n_gui_color_picker_is_open(const N_GUI_COLOR_PICKER* picker);

int n_gui_color_picker_get(const N_GUI_COLOR_PICKER* picker, float rgba[4]);

int n_gui_color_picker_window(const N_GUI_COLOR_PICKER* picker);

int n_gui_color_picker_slider(const N_GUI_COLOR_PICKER* picker, int channel);

void n_gui_color_picker_free(N_GUI_COLOR_PICKER** picker);

void n_gui_draw_color_swatch(float x, float y, float w, float h, const float rgba[4]);

void n_gui_color_to_hex(const float rgba[4], char* out, size_t len);

ALLEGRO_MOUSE_CURSOR* n_gui_eyedropper_cursor(float scale);


#ifdef __cplusplus
}
#endif
#endif /* HAVE_ALLEGRO */
#endif /* __NILOREA_GUI_COLOR__ */