| 1 | const std = @import("std"); |
| 2 | const color = @import("color"); |
| 3 | const expect = @import("expect").expect; |
| 4 | |
| 5 | test { |
| 6 | // https://www.colorhexa.com/4fc3f7 |
| 7 | // https://encycolorpedia.com/4fc3f7 |
| 8 | const o = color.sRGB.parseHexConst("#4FC3F7"); |
| 9 | { |
| 10 | const r, const g, const b, const a = o.to_array(); |
| 11 | try std.testing.expect(r == 79); |
| 12 | try std.testing.expect(g == 195); |
| 13 | try std.testing.expect(b == 247); |
| 14 | try std.testing.expect(a == 255); |
| 15 | try expect(o.to_array()).toEqual(.{ 79, 195, 247, 255 }); |
| 16 | } |
| 17 | { |
| 18 | // 68% cyan, 21.1% magenta, 0% yellow and 3.1% black |
| 19 | // cyan: 68% (0.68), magenta: 21% (0.211), yellow: 0% (0.0), key: 3% (0.031) |
| 20 | const c, const m, const y, const k, const a = o.to_cmyk().to_array(); |
| 21 | try std.testing.expectApproxEqAbs(0.68, c, 0.01); |
| 22 | try std.testing.expectApproxEqAbs(0.21, m, 0.01); |
| 23 | try std.testing.expectApproxEqAbs(0.00, y, 0.01); |
| 24 | try std.testing.expectApproxEqAbs(0.03, k, 0.01); |
| 25 | try std.testing.expect(a == 1.0); |
| 26 | try expect(o.to_cmyk().to_srgb().to_array()).toEqual(.{ 79, 195, 247, 255 }); |
| 27 | } |
| 28 | { |
| 29 | // 198.6°, 91.3, 63.9 |
| 30 | const h, const s, const l, const a = o.to_hsl().to_array(); |
| 31 | try std.testing.expectApproxEqAbs(0.552, h, 0.001); |
| 32 | try std.testing.expectApproxEqAbs(0.913, s, 0.001); |
| 33 | try std.testing.expectApproxEqAbs(0.639, l, 0.001); |
| 34 | try std.testing.expect(a == 1.0); |
| 35 | try expect(o.to_hsl().to_srgb().to_array()).toEqual(.{ 79, 195, 247, 255 }); |
| 36 | } |
| 37 | { |
| 38 | // 198.6°, 68, 96.9 |
| 39 | const h, const s, const v, const a = o.to_hsv().to_array(); |
| 40 | try std.testing.expectApproxEqAbs(0.552, h, 0.001); |
| 41 | try std.testing.expectApproxEqAbs(0.680, s, 0.001); |
| 42 | try std.testing.expectApproxEqAbs(0.969, v, 0.001); |
| 43 | try std.testing.expect(a == 1.0); |
| 44 | try expect(o.to_hsv().to_srgb().to_array()).toEqual(.{ 79, 195, 247, 255 }); |
| 45 | } |
| 46 | { |
| 47 | // 0.07818742180518633 0.5457244613701866 0.9301108583754237 |
| 48 | // 0.0781874218051863, 0.545724461370187, 0.930110858375424 |
| 49 | const r, const g, const b, const a = o.to_linear_rgb().to_array(); |
| 50 | try std.testing.expectApproxEqAbs(0.078, r, 0.001); |
| 51 | try std.testing.expectApproxEqAbs(0.545, g, 0.001); |
| 52 | try std.testing.expectApproxEqAbs(0.930, b, 0.001); |
| 53 | try std.testing.expect(a == 1.0); |
| 54 | // try expect(o.to_linear_rgb().to_srgb().to_array()).toEqual(.{ 79, 195, 247, 255 }); // https://github.com/ziglang/zig/issues/22354 |
| 55 | try std.testing.expectApproxEqAbs(0.474, o.to_linear_rgb().relative_luminance(), 0.001); |
| 56 | } |
| 57 | { |
| 58 | // Y: 158.789, Cb: 167.996, Cr: 73.384 |
| 59 | try expect(o.to_ycbcr().to_array()).toEqual(.{ 166, 173, 65, 255 }); |
| 60 | try expect(o.to_ycbcr().to_srgb().to_array()).toEqual(.{ 77, 195, 245, 255 }); |
| 61 | } |
| 62 | { |
| 63 | // Y: 166.244, U: 39.741, V: -76.541 |
| 64 | const y, const u, const v, const a = o.to_yuv().normal(); |
| 65 | try std.testing.expectApproxEqAbs(166.244, y, 0.001); |
| 66 | try std.testing.expectApproxEqAbs(39.723, u, 0.001); |
| 67 | try std.testing.expectApproxEqAbs(-76.539, v, 0.001); |
| 68 | try std.testing.expect(a == 1.0); |
| 69 | try expect(o.to_yuv().to_srgb().to_array()).toEqual(.{ 78, 195, 246, 255 }); |
| 70 | } |
| 71 | } |