DVUI

colorPicker

A photoshop style color picker

Returns true of the color was changed

Parameters

#
src:std.builtin.SourceLocation
std.builtin.SourceLocation
init_opts:ColorPickerInitOptions
ColorPickerInitOptions
opts:Options
Options

Source

Implementation

#
pub fn colorPicker(src: std.builtin.SourceLocation, init_opts: ColorPickerInitOptions, opts: Options) bool {
    var picker: ColorPickerWidget = undefined;
    picker.init(src, .{ .dir = init_opts.dir, .hsv = init_opts.hsv }, opts);
    defer picker.deinit();

    var changed = picker.color_changed;
    var rgb = init_opts.hsv.toColor();

    var side_box = dvui.box(@src(), .{}, .{});
    defer side_box.deinit();

    const slider_expand = Options.Expand.fromDirection(.horizontal);
    switch (init_opts.sliders) {
        .rgb => {
            var r: f32 = rgb.r;
            var g: f32 = rgb.g;
            var b: f32 = rgb.b;
            var a: f32 = rgb.a;

            var slider_changed = false;
            if (dvui.sliderEntry(@src(), "R: {d:0.0}", .{ .value = &r, .min = 0, .max = 255, .interval = 1 }, .{ .expand = slider_expand })) {
                slider_changed = true;
            }
            if (dvui.sliderEntry(@src(), "G: {d:0.0}", .{ .value = &g, .min = 0, .max = 255, .interval = 1 }, .{ .expand = slider_expand })) {
                slider_changed = true;
            }
            if (dvui.sliderEntry(@src(), "B: {d:0.0}", .{ .value = &b, .min = 0, .max = 255, .interval = 1 }, .{ .expand = slider_expand })) {
                slider_changed = true;
            }
            if (init_opts.alpha and dvui.sliderEntry(@src(), "A: {d:0.0}", .{ .value = &a, .min = 0, .max = 255, .interval = 1 }, .{ .expand = slider_expand })) {
                slider_changed = true;
            }
            if (slider_changed) {
                init_opts.hsv.* = .fromColor(.{ .r = @trunc(r), .g = @trunc(g), .b = @trunc(b), .a = @trunc(a) });
                changed = true;
            }
        },
        .hsv => {
            if (dvui.sliderEntry(@src(), "H: {d:0.0}", .{ .value = &init_opts.hsv.h, .min = 0, .max = 359.99, .interval = 1 }, .{ .expand = slider_expand })) {
                changed = true;
            }
            if (dvui.sliderEntry(@src(), "S: {d:0.2}", .{ .value = &init_opts.hsv.s, .min = 0, .max = 1, .interval = 0.01 }, .{ .expand = slider_expand })) {
                changed = true;
            }
            if (dvui.sliderEntry(@src(), "V: {d:0.2}", .{ .value = &init_opts.hsv.v, .min = 0, .max = 1, .interval = 0.01 }, .{ .expand = slider_expand })) {
                changed = true;
            }
            if (init_opts.alpha and dvui.sliderEntry(@src(), "A: {d:0.2}", .{ .value = &init_opts.hsv.a, .min = 0, .max = 1, .interval = 0.01 }, .{ .expand = slider_expand })) {
                changed = true;
            }
        },
    }

    if (init_opts.hex_text_entry) {
        const res = textEntryColor(@src(), .{ .allow_alpha = init_opts.alpha, .value = &rgb }, .{ .expand = slider_expand });
        if (res.changed) {
            init_opts.hsv.* = .fromColor(rgb);
            changed = true;
        }
    }

    return changed;
}