Skip to content

Commit

Permalink
colorspace: add luma to RGBA and update toGray method
Browse files Browse the repository at this point in the history
  • Loading branch information
arrufat committed Jan 7, 2025
1 parent a60a37b commit b02c32f
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/colorspace.zig
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,12 @@ pub const Rgba = packed struct {
};
}

/// Luma is the weighted average of gamma-corrected R, G, and B, based on their contribution
/// to perceived lightness. This implementation uses the the Rec. 709 for sRGB.
pub fn luma(self: Rgba) f64 {
return self.toRgbFloat().luma();
}

/// Alpha-blends color into self.
pub fn blend(self: *Rgba, color: Rgba) void {
alphaBlend(Rgba, self, color);
Expand All @@ -381,9 +387,9 @@ pub const Rgba = packed struct {
return self.r == self.g and self.g == self.b;
}

/// Converts the RGBA color into grayscale.
/// Converts the RGB color into grayscale using luma.
pub fn toGray(self: Rgba) u8 {
return @intFromFloat(@round(self.toHsl().l / 100 * 255));
return @intFromFloat(self.luma() * 255);
}

/// Converts the RGBA color into a hex value.
Expand Down

0 comments on commit b02c32f

Please # to comment.