Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Theme type that is more easy to edit in a configuration file #140

Merged
merged 18 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions examples/raylib-ontop.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const std = @import("std");
const dvui = @import("dvui");
comptime { std.debug.assert(dvui.backend_kind == .raylib); }
comptime {
std.debug.assert(dvui.backend_kind == .raylib);
}
const RaylibBackend = dvui.backend;
const ray = RaylibBackend.c;

Expand Down Expand Up @@ -29,7 +31,7 @@ pub fn main() !void {

// init dvui Window (maps onto a single OS window)
// OS window is managed by raylib, not dvui
var win = try dvui.Window.init(@src(), gpa, backend.backend(), .{ .theme = &dvui.Theme.Jungle });
var win = try dvui.Window.init(@src(), gpa, backend.backend(), .{});
defer win.deinit();

var selected_color: dvui.Color = dvui.Color.white;
Expand Down
41 changes: 39 additions & 2 deletions src/Color.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ g: u8 = 0xff,
b: u8 = 0xff,
a: u8 = 0xff,

/// Returns brightness of the color as a value between 0 and 1
pub fn brightness(self: @This()) f32 {
const red: f32 = @as(f32, @floatFromInt(self.r)) / 255.0;
const green: f32 = @as(f32, @floatFromInt(self.g)) / 255.0;
const blue: f32 = @as(f32, @floatFromInt(self.b)) / 255.0;

return 0.2126 * red + 0.7152 * green + 0.0722 * blue;
}

pub const HSLuv = struct {
h: f32 = 0.0,
s: f32 = 100.0,
Expand Down Expand Up @@ -83,7 +92,9 @@ pub fn extract(self: Color, field: FieldEnum) u16 {
.r => self.r,
.g => self.g,
.b => self.b,
.a => @compileError("cannot extract alpha field from color"),
.a => {
@panic("This should never be called");
},
});
const result = normalized_a * value;
return @intFromFloat(@floor(result));
Expand All @@ -99,8 +110,34 @@ pub fn alphaAdd(self: Color, other: Color) Color {
};
}

pub fn toHexString(self: Color) ![7]u8 {
/// Adds two colors rgb component-wise premultiplied by alpha
pub fn alphaAverage(self: Color, other: Color) Color {
return Color{
.r = @intCast((self.extract(.r) + other.extract(.r)) / (255 * 2)),
.g = @intCast((self.extract(.g) + other.extract(.g)) / (255 * 2)),
.b = @intCast((self.extract(.b) + other.extract(.b)) / (255 * 2)),
.a = 255,
};
}

pub const HexString = [7]u8;

pub fn toHexString(self: Color) !HexString {
var result: [7]u8 = .{0} ** 7;
_ = try std.fmt.bufPrint(&result, "#{x:0>2}{x:0>2}{x:0>2}", .{ self.r, self.g, self.b });
return result;
}

/// Converts slice of HexString to Color
pub fn fromHex(hex: HexString) !Color {
//if (hex[0] != '#') return error.NotAColor;
//if (hex.len != 7) return error.WrongStringLength;

const num: u24 = try std.fmt.parseInt(u24, hex[1..], 16);
const result = Color{
.r = @intCast(num >> 16 & 0xff),
.g = @intCast(num >> 8 & 0xff),
.b = @intCast(num & 0xff),
};
return result;
}
60 changes: 7 additions & 53 deletions src/Examples.zig
Original file line number Diff line number Diff line change
Expand Up @@ -267,28 +267,7 @@ pub fn demo() !void {
dvui.toggleDebugWindow();
}

const theme_choice: usize = blk: {
for (dvui.Theme.ptrs, 0..) |tptr, i| {
if (dvui.themeGet() == tptr) {
break :blk i;
}
}
break :blk 0;
};

var dd = dvui.DropdownWidget.init(@src(), .{ .selected_index = theme_choice, .label = dvui.themeGet().name }, .{ .min_size_content = .{ .w = 120 } });
try dd.install();

if (try dd.dropped()) {
for (dvui.Theme.ptrs) |tptr| {
if (try dd.addChoiceLabel(tptr.name)) {
dvui.themeSet(tptr);
break;
}
}
}

dd.deinit();
try dvui.currentWindow().themes.picker(@src());
}

{
Expand Down Expand Up @@ -364,10 +343,10 @@ pub fn demo() !void {
try animations();
}

if (try dvui.expander(@src(), "Theme Serialization and Parsing", .{}, .{ .expand = .horizontal })) {
if (try dvui.expander(@src(), "Theme Parsing", .{}, .{ .expand = .horizontal })) {
var b = try dvui.box(@src(), .vertical, .{ .expand = .horizontal, .margin = .{ .x = 10 } });
defer b.deinit();
try themeSerialization(float.data().id);
try themeSerialization();
}

if (try dvui.expander(@src(), "Struct UI Widget (Experimental)", .{}, .{ .expand = .horizontal })) {
Expand Down Expand Up @@ -459,36 +438,11 @@ pub fn demo() !void {
}
}

pub fn themeSerialization(demo_win_id: u32) !void {
{
var serialize_box = try dvui.box(@src(), .vertical, .{ .expand = .horizontal, .margin = .{ .x = 10 } });
defer serialize_box.deinit();
pub fn themeSerialization() !void {
var serialize_box = try dvui.box(@src(), .vertical, .{ .expand = .horizontal, .margin = .{ .x = 10 } });
defer serialize_box.deinit();

// Demonstrate serializing a theme
const Static = struct {
var bytes: [4096]u8 = undefined;
var buffer = std.io.fixedBufferStream(&bytes);
};

if (try dvui.button(@src(), "Serialize Active Theme", .{}, .{})) {
Static.buffer.reset();
_ = try std.json.stringify(dvui.themeGet(), .{}, Static.buffer.writer());
}

if (try dvui.expander(@src(), "Serialized Theme", .{}, .{ .expand = .horizontal })) {
var b = try dvui.box(@src(), .vertical, .{ .expand = .horizontal, .margin = .{ .x = 10 } });
defer b.deinit();

if (try dvui.button(@src(), "Copy To Clipboard", .{}, .{})) {
try dvui.clipboardTextSet(Static.buffer.getWritten());
try dvui.toast(@src(), .{ .subwindow_id = demo_win_id, .message = "Copied!" });
}

var tl = try dvui.textLayout(@src(), .{}, .{ .expand = .horizontal, .background = false });
try tl.addText(Static.buffer.getWritten(), .{});
tl.deinit();
}
}
try dvui.labelNoFmt(@src(), "TODO: demonstrate loading a quicktheme here", .{});
}

pub fn basicWidgets() !void {
Expand Down
Loading