mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-12-14 23:38:57 +00:00
add color representation by RGBA
It seems we can represent most things with RGBA (at least this is what other browsers do) so a universal color API based on RGBA is nice to have, especially for CSS and Canvas.
This commit is contained in:
111
src/browser/cssom/color.zig
Normal file
111
src/browser/cssom/color.zig
Normal file
@@ -0,0 +1,111 @@
|
||||
// Copyright (C) 2023-2025 Lightpanda (Selecy SAS)
|
||||
//
|
||||
// Francis Bouvier <francis@lightpanda.io>
|
||||
// Pierre Tachoire <pierre@lightpanda.io>
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as
|
||||
// published by the Free Software Foundation, either version 3 of the
|
||||
// License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
const std = @import("std");
|
||||
const Io = std.Io;
|
||||
|
||||
pub const RGBA = packed struct(u32) {
|
||||
r: u8,
|
||||
g: u8,
|
||||
b: u8,
|
||||
a: u8 = std.math.maxInt(u8),
|
||||
|
||||
pub fn init(r: u8, g: u8, b: u8, a: f32) RGBA {
|
||||
const clamped = std.math.clamp(a, 0, 1);
|
||||
return .{ .r = r, .g = g, .b = b, .a = @intFromFloat(clamped * 255) };
|
||||
}
|
||||
|
||||
/// Initializes a `Color` by parsing the given HEX.
|
||||
/// HEX is either represented as RGB or RGBA by `Color`.
|
||||
pub fn initFromHex(hex: []const u8) !RGBA {
|
||||
// HEX is bit weird; its length (hash omitted) can be 3, 4, 6 or 8.
|
||||
// The parsing gets a bit different depending on it.
|
||||
const slice = hex[1..];
|
||||
switch (slice.len) {
|
||||
// This means the digit for a color is repeated.
|
||||
// Given HEX is #f0c, its interpreted the same as #FF00CC.
|
||||
3 => {
|
||||
const r = try std.fmt.parseInt(u8, &.{ slice[0], slice[0] }, 16);
|
||||
const g = try std.fmt.parseInt(u8, &.{ slice[1], slice[1] }, 16);
|
||||
const b = try std.fmt.parseInt(u8, &.{ slice[2], slice[2] }, 16);
|
||||
return .{ .r = r, .g = g, .b = b, .a = 255 };
|
||||
},
|
||||
4 => {
|
||||
const r = try std.fmt.parseInt(u8, &.{ slice[0], slice[0] }, 16);
|
||||
const g = try std.fmt.parseInt(u8, &.{ slice[1], slice[1] }, 16);
|
||||
const b = try std.fmt.parseInt(u8, &.{ slice[2], slice[2] }, 16);
|
||||
const a = try std.fmt.parseInt(u8, &.{ slice[3], slice[3] }, 16);
|
||||
return .{ .r = r, .g = g, .b = b, .a = a };
|
||||
},
|
||||
// Regular HEX format.
|
||||
6 => {
|
||||
const r = try std.fmt.parseInt(u8, slice[0..2], 16);
|
||||
const g = try std.fmt.parseInt(u8, slice[2..4], 16);
|
||||
const b = try std.fmt.parseInt(u8, slice[4..6], 16);
|
||||
return .{ .r = r, .g = g, .b = b, .a = 255 };
|
||||
},
|
||||
8 => {
|
||||
const r = try std.fmt.parseInt(u8, slice[0..2], 16);
|
||||
const g = try std.fmt.parseInt(u8, slice[2..4], 16);
|
||||
const b = try std.fmt.parseInt(u8, slice[4..6], 16);
|
||||
const a = try std.fmt.parseInt(u8, slice[6..8], 16);
|
||||
return .{ .r = r, .g = g, .b = b, .a = a };
|
||||
},
|
||||
else => unreachable,
|
||||
}
|
||||
}
|
||||
|
||||
/// By default, browsers prefer lowercase formatting.
|
||||
const format_upper = false;
|
||||
|
||||
/// Formats the `Color` according to web expectations.
|
||||
/// If color is opaque, HEX is preferred; RGBA otherwise.
|
||||
pub fn format(self: *const RGBA, writer: *Io.Writer) Io.Writer.Error!void {
|
||||
if (self.isOpaque()) {
|
||||
// Convert RGB to HEX.
|
||||
// https://gristle.tripod.com/hexconv.html
|
||||
// Hexadecimal characters up to 15.
|
||||
const char: []const u8 = "0123456789" ++ if (format_upper) "ABCDEF" else "abcdef";
|
||||
// This variant always prefers 6 digit format, +1 is for hash char.
|
||||
const buffer = [7]u8{
|
||||
'#',
|
||||
char[self.r >> 4],
|
||||
char[self.r & 15],
|
||||
char[self.g >> 4],
|
||||
char[self.g & 15],
|
||||
char[self.b >> 4],
|
||||
char[self.b & 15],
|
||||
};
|
||||
|
||||
return writer.writeAll(&buffer);
|
||||
}
|
||||
|
||||
// Prefer RGBA format for everything else.
|
||||
return writer.print("rgba({d}, {d}, {d}, {d:.2})", .{ self.r, self.g, self.b, self.normalizedAlpha() });
|
||||
}
|
||||
|
||||
/// Returns true if `Color` is opaque.
|
||||
pub inline fn isOpaque(self: *const RGBA) bool {
|
||||
return self.a == std.math.maxInt(u8);
|
||||
}
|
||||
|
||||
/// Returns the normalized alpha value.
|
||||
pub inline fn normalizedAlpha(self: *const RGBA) f32 {
|
||||
return @as(f32, @floatFromInt(self.a)) / 255;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user