summaryrefslogtreecommitdiff
path: root/graphics.c
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2025-01-16 03:38:24 +0100
committerMitya Selivanov <automainint@guattari.tech>2025-01-16 03:38:24 +0100
commit725662a623ac36d44d2b44c568d8dd6c3eeeeca1 (patch)
tree50afdc84553f44ab0dc7dff425752e87f0a5e661 /graphics.c
parentc6c72f54ea7f25defa7d39216f7713c46b873362 (diff)
downloadreduced_system_layer-725662a623ac36d44d2b44c568d8dd6c3eeeeca1.zip
Drag and drop impl on X11 (not tested)
Diffstat (limited to 'graphics.c')
-rw-r--r--graphics.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/graphics.c b/graphics.c
index 2431e8c..6f786c5 100644
--- a/graphics.c
+++ b/graphics.c
@@ -51,6 +51,11 @@ vec3_f32 lch_from_rgb(vec3_f32 rgb);
vec4_f32 with_alpha(vec3_f32 color, f32 alpha);
vec3_f32 without_alpha(vec4_f32 color);
+u32 rgb_u32_from_f32(vec3_f32 color);
+vec3_f32 rgb_f32_from_u32(u32 color);
+u32 rgba_u32_from_f32(vec4_f32 color);
+vec4_f32 rgba_f32_from_u32(u32 color);
+
#define RGB(...) ((Brush) { .color = { __VA_ARGS__, 1.f } })
#define RGBA(...) ((Brush) { .color = { __VA_ARGS__ } })
#define LCH(...) ((Brush) { .color = with_alpha(gamma_apply(rgb_from_lch((vec3_f32) { __VA_ARGS__ })), 1.f) } })
@@ -442,6 +447,49 @@ vec3_f32 without_alpha(vec4_f32 color) {
};
}
+u32 rgb_u32_from_f32(vec3_f32 color) {
+ i32 ir = (i32) floor(color.x * 255. + .5);
+ i32 ig = (i32) floor(color.y * 255. + .5);
+ i32 ib = (i32) floor(color.z * 255. + .5);
+
+ u32 r = ir < 0 ? 0u : ir > 255 ? 255u : (u32) ir;
+ u32 g = ig < 0 ? 0u : ig > 255 ? 255u : (u32) ig;
+ u32 b = ib < 0 ? 0u : ib > 255 ? 255u : (u32) ib;
+
+ return (r << 16) | (g << 8) | b;
+}
+
+vec3_f32 rgb_f32_from_u32(u32 color) {
+ return (vec3_f32) {
+ .x = ((color & 0xff0000) >> 16) / 255.f,
+ .y = ((color & 0x00ff00) >> 8) / 255.f,
+ .z = (color & 0x0000ff) / 255.f,
+ };
+}
+
+u32 rgba_u32_from_f32(vec4_f32 color) {
+ i32 ir = (i32) floor(color.x * 255. + .5);
+ i32 ig = (i32) floor(color.y * 255. + .5);
+ i32 ib = (i32) floor(color.z * 255. + .5);
+ i32 ia = (i32) floor(color.w * 255. + .5);
+
+ u32 r = ir < 0 ? 0u : ir > 255 ? 255u : (u32) ir;
+ u32 g = ig < 0 ? 0u : ig > 255 ? 255u : (u32) ig;
+ u32 b = ib < 0 ? 0u : ib > 255 ? 255u : (u32) ib;
+ u32 a = ia < 0 ? 0u : ia > 255 ? 255u : (u32) ia;
+
+ return (a << 24) | (r << 16) | (g << 8) | b;
+}
+
+vec4_f32 rgba_f32_from_u32(u32 color) {
+ return (vec4_f32) {
+ .x = ((color & 0x00ff0000) >> 16) / 255.f,
+ .y = ((color & 0x0000ff00) >> 8) / 255.f,
+ .z = (color & 0x000000ff) / 255.f,
+ .w = ((color & 0xff000000) >> 24) / 255.f,
+ };
+}
+
b8 rectangle_contains(f64 x0, f64 y0, f64 width, f64 height, f64 px, f64 py) {
return px >= x0 && px < x0 + width && py >= y0 && py < y0 + height;
}