Something went wrong. Try again.
Charm-like tui library
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253const std = @import("std");const utils = @import("utils.zig");const LAB = @import("LAB.zig");const HSV = @import("HSV.zig");
const RGB = @This();
r: u8,g: u8,b: u8,
pub fn toHsv(self: RGB) HSV { const r = @as(f64, @floatFromInt(self.r)) / 255.0; const g = @as(f64, @floatFromInt(self.g)) / 255.0; const b = @as(f64, @floatFromInt(self.b)) / 255.0; const mx = @max(r, @max(g, b)); const mn = @min(r, @min(g, b)); const d = mx - mn; var h: f64 = 0; if (d != 0) { if (mx == r) { h = 60.0 * @mod((g - b) / d, 6.0); } else if (mx == g) { h = 60.0 * ((b - r) / d + 2.0); } else { h = 60.0 * ((r - g) / d + 4.0); } } if (h < 0) h += 360.0; return .{ .h = h, .s = if (mx == 0) 0 else d / mx, .v = mx, };}
pub fn toLab(self: RGB) LAB { const r = utils.srgbToLinear(self.r); const g = utils.srgbToLinear(self.g); const b = utils.srgbToLinear(self.b); const x = 0.4124564 * r + 0.3575761 * g + 0.1804375 * b; const y = 0.2126729 * r + 0.7151522 * g + 0.0721750 * b; const z = 0.0193339 * r + 0.1191920 * g + 0.9503041 * b; const fx = utils.labF(x / utils.xn); const fy = utils.labF(y / utils.yn); const fz = utils.labF(z / utils.zn); return .{ .l = 116.0 * fy - 16.0, .a = 500.0 * (fx - fy), .b = 200.0 * (fy - fz), };}