abachrome 0.1.6 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +4 -0
- data/lib/abachrome/abc_decimal.rb +4 -0
- data/lib/abachrome/color.rb +72 -0
- data/lib/abachrome/color_mixins/harmonies.rb +187 -0
- data/lib/abachrome/color_mixins/to_lms.rb +106 -0
- data/lib/abachrome/color_mixins/to_xyz.rb +106 -0
- data/lib/abachrome/color_mixins/wcag.rb +126 -0
- data/lib/abachrome/color_models/cmyk.rb +2 -1
- data/lib/abachrome/color_models/xyz.rb +8 -0
- data/lib/abachrome/color_space.rb +12 -0
- data/lib/abachrome/converters/hsl_to_srgb.rb +73 -0
- data/lib/abachrome/converters/hwb_to_srgb.rb +66 -0
- data/lib/abachrome/converters/lab_to_srgb.rb +38 -0
- data/lib/abachrome/converters/lab_to_xyz.rb +73 -0
- data/lib/abachrome/converters/lch_to_lab.rb +50 -0
- data/lib/abachrome/converters/lch_to_srgb.rb +34 -0
- data/lib/abachrome/converters/lms_to_oklab.rb +28 -0
- data/lib/abachrome/converters/lrgb_to_lms.rb +26 -0
- data/lib/abachrome/converters/oklab_to_xyz.rb +29 -0
- data/lib/abachrome/converters/oklch_to_lms.rb +28 -0
- data/lib/abachrome/converters/srgb_to_lms.rb +29 -0
- data/lib/abachrome/converters/srgb_to_xyz.rb +44 -0
- data/lib/abachrome/converters/xyz_to_lrgb.rb +46 -0
- data/lib/abachrome/converters/xyz_to_srgb.rb +30 -0
- data/lib/abachrome/floatify.rb +282 -0
- data/lib/abachrome/named/css.rb +297 -147
- data/lib/abachrome/parsers/css.rb +65 -162
- data/lib/abachrome/version.rb +1 -1
- data/lib/abachrome.rb +57 -7
- metadata +19 -1
|
@@ -153,12 +153,24 @@ module Abachrome
|
|
|
153
153
|
s.color_model = :hsl
|
|
154
154
|
end
|
|
155
155
|
|
|
156
|
+
ColorSpace.register(:hwb) do |s|
|
|
157
|
+
s.coordinates = %i[hue whiteness blackness]
|
|
158
|
+
s.white_point = :D65
|
|
159
|
+
s.color_model = :hwb
|
|
160
|
+
end
|
|
161
|
+
|
|
156
162
|
ColorSpace.register(:lab) do |s|
|
|
157
163
|
s.coordinates = %i[lightness a b]
|
|
158
164
|
s.white_point = :D65
|
|
159
165
|
s.color_model = :lab
|
|
160
166
|
end
|
|
161
167
|
|
|
168
|
+
ColorSpace.register(:lch) do |s|
|
|
169
|
+
s.coordinates = %i[lightness chroma hue]
|
|
170
|
+
s.white_point = :D65
|
|
171
|
+
s.color_model = :lch
|
|
172
|
+
end
|
|
173
|
+
|
|
162
174
|
ColorSpace.register(:oklab) do |s|
|
|
163
175
|
s.coordinates = %i[lightness a b]
|
|
164
176
|
s.white_point = :D65
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# Abachrome::Converters::HslToSrgb - HSL to sRGB color space converter
|
|
2
|
+
#
|
|
3
|
+
# This converter transforms colors from the HSL (hue, saturation, lightness) color space to
|
|
4
|
+
# the standard RGB (sRGB) color space using the chroma-based algorithm defined by the CSS
|
|
5
|
+
# Color specification. The hue angle selects one of six sectors of the color wheel, and the
|
|
6
|
+
# resulting base color is offset by a lightness match value to produce the final sRGB
|
|
7
|
+
# coordinates.
|
|
8
|
+
#
|
|
9
|
+
# Key features:
|
|
10
|
+
# - Implements the standard CSS HSL to sRGB conversion algorithm
|
|
11
|
+
# - Normalizes the hue angle into the 0-360 degree range before sector selection
|
|
12
|
+
# - Derives chroma from saturation and lightness for accurate color reproduction
|
|
13
|
+
# - Maintains alpha channel transparency values during conversion
|
|
14
|
+
# - Uses AbcDecimal arithmetic for precise color science calculations
|
|
15
|
+
# - Validates input color space to ensure proper HSL source data
|
|
16
|
+
#
|
|
17
|
+
# HSL is the color model used by the CSS hsl() and hsla() functions, expressing colors in
|
|
18
|
+
# terms that map closely to intuitive descriptions of hue, vividness, and brightness.
|
|
19
|
+
|
|
20
|
+
module Abachrome
|
|
21
|
+
module Converters
|
|
22
|
+
class HslToSrgb < Abachrome::Converters::Base
|
|
23
|
+
# Converts a color from HSL color space to sRGB color space.
|
|
24
|
+
#
|
|
25
|
+
# @param hsl_color [Abachrome::Color] The color in HSL color space to convert
|
|
26
|
+
# @return [Abachrome::Color] A new Color object in sRGB color space with the converted coordinates
|
|
27
|
+
# @raise [RuntimeError] If the provided color is not in HSL color space
|
|
28
|
+
def self.convert(hsl_color)
|
|
29
|
+
raise_unless hsl_color, :hsl
|
|
30
|
+
|
|
31
|
+
h, s, l = hsl_color.coordinates.map { |_| AbcDecimal(_) }
|
|
32
|
+
|
|
33
|
+
Color.new(
|
|
34
|
+
ColorSpace.find(:srgb),
|
|
35
|
+
to_srgb(h, s, l),
|
|
36
|
+
hsl_color.alpha
|
|
37
|
+
)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Converts hue, saturation, and lightness components into sRGB coordinates.
|
|
41
|
+
#
|
|
42
|
+
# The hue is normalized into the 0-360 degree range and divided into six 60-degree
|
|
43
|
+
# sectors. Chroma is scaled by saturation and by the distance of lightness from the
|
|
44
|
+
# extremes, and the intermediate component is interpolated within the active sector.
|
|
45
|
+
#
|
|
46
|
+
# @param h [AbcDecimal] The hue angle in degrees
|
|
47
|
+
# @param s [AbcDecimal] The saturation component, in range 0..1
|
|
48
|
+
# @param l [AbcDecimal] The lightness component, in range 0..1
|
|
49
|
+
# @return [Array<AbcDecimal>] The red, green, and blue components, in range 0..1
|
|
50
|
+
def self.to_srgb(h, s, l)
|
|
51
|
+
h = h % AD(360)
|
|
52
|
+
h_prime = h / AD(60)
|
|
53
|
+
|
|
54
|
+
c = (AD(1) - (((AD(2) * l) - AD(1)).abs)) * s
|
|
55
|
+
x = c * (AD(1) - ((h_prime % AD(2)) - AD(1)).abs)
|
|
56
|
+
m = l - (c / AD(2))
|
|
57
|
+
|
|
58
|
+
r, g, b = case h_prime.to_f
|
|
59
|
+
when 0...1 then [c, x, AD(0)]
|
|
60
|
+
when 1...2 then [x, c, AD(0)]
|
|
61
|
+
when 2...3 then [AD(0), c, x]
|
|
62
|
+
when 3...4 then [AD(0), x, c]
|
|
63
|
+
when 4...5 then [x, AD(0), c]
|
|
64
|
+
else [c, AD(0), x]
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
[r + m, g + m, b + m]
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# Abachrome::Converters::HwbToSrgb - HWB to sRGB color space converter
|
|
2
|
+
#
|
|
3
|
+
# This converter transforms colors from the HWB (hue, whiteness, blackness) color space to
|
|
4
|
+
# the standard RGB (sRGB) color space following the algorithm defined by the CSS Color
|
|
5
|
+
# specification. The hue component selects a fully saturated base color, which is then mixed
|
|
6
|
+
# with white and black according to the whiteness and blackness components.
|
|
7
|
+
#
|
|
8
|
+
# Key features:
|
|
9
|
+
# - Implements the standard CSS HWB to sRGB conversion algorithm
|
|
10
|
+
# - Normalizes whiteness and blackness proportionally when their sum exceeds one
|
|
11
|
+
# - Produces an achromatic gray when whiteness and blackness fully saturate the color
|
|
12
|
+
# - Derives the pure hue color through the HSL conversion at full saturation
|
|
13
|
+
# - Maintains alpha channel transparency values during conversion
|
|
14
|
+
# - Uses AbcDecimal arithmetic for precise color science calculations
|
|
15
|
+
# - Validates input color space to ensure proper HWB source data
|
|
16
|
+
#
|
|
17
|
+
# HWB is the color model used by the CSS hwb() function, describing colors in terms of how
|
|
18
|
+
# much white and black are mixed into a pure hue, which maps closely to how pigments are
|
|
19
|
+
# mixed in practice.
|
|
20
|
+
|
|
21
|
+
module Abachrome
|
|
22
|
+
module Converters
|
|
23
|
+
class HwbToSrgb < Abachrome::Converters::Base
|
|
24
|
+
# Converts a color from HWB color space to sRGB color space.
|
|
25
|
+
#
|
|
26
|
+
# @param hwb_color [Abachrome::Color] The color in HWB color space to convert
|
|
27
|
+
# @return [Abachrome::Color] A new Color object in sRGB color space with the converted coordinates
|
|
28
|
+
# @raise [RuntimeError] If the provided color is not in HWB color space
|
|
29
|
+
def self.convert(hwb_color)
|
|
30
|
+
raise_unless hwb_color, :hwb
|
|
31
|
+
|
|
32
|
+
h, w, b = hwb_color.coordinates.map { |_| AbcDecimal(_) }
|
|
33
|
+
|
|
34
|
+
Color.new(
|
|
35
|
+
ColorSpace.find(:srgb),
|
|
36
|
+
to_srgb(h, w, b),
|
|
37
|
+
hwb_color.alpha
|
|
38
|
+
)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Converts hue, whiteness, and blackness components into sRGB coordinates.
|
|
42
|
+
#
|
|
43
|
+
# When whiteness and blackness sum to one or more the result is achromatic, and the
|
|
44
|
+
# gray level is the whiteness expressed as a proportion of the total. Otherwise the
|
|
45
|
+
# fully saturated hue color is scaled into the range left between the two.
|
|
46
|
+
#
|
|
47
|
+
# @param h [AbcDecimal] The hue angle in degrees
|
|
48
|
+
# @param w [AbcDecimal] The whiteness component, in range 0..1
|
|
49
|
+
# @param b [AbcDecimal] The blackness component, in range 0..1
|
|
50
|
+
# @return [Array<AbcDecimal>] The red, green, and blue components, in range 0..1
|
|
51
|
+
def self.to_srgb(h, w, b)
|
|
52
|
+
total = w + b
|
|
53
|
+
|
|
54
|
+
if total >= AD(1)
|
|
55
|
+
gray = w / total
|
|
56
|
+
return [gray, gray, gray]
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
scale = AD(1) - total
|
|
60
|
+
HslToSrgb.to_srgb(h, AD(1), AD("0.5")).map { |c| (c * scale) + w }
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Abachrome::Converters::LabToSrgb - CIELAB to sRGB color space converter
|
|
2
|
+
#
|
|
3
|
+
# This converter transforms colors from the CIELAB color space to the standard RGB (sRGB)
|
|
4
|
+
# color space by routing through the intermediate XYZ and linear RGB color spaces. The
|
|
5
|
+
# CIELAB values are first converted to XYZ using the reference white point, then to linear
|
|
6
|
+
# RGB through the sRGB primaries matrix, and finally gamma corrected into sRGB.
|
|
7
|
+
#
|
|
8
|
+
# Key features:
|
|
9
|
+
# - Composes the CIELAB to XYZ, XYZ to linear RGB, and linear RGB to sRGB conversions
|
|
10
|
+
# - Reuses the shared gamma correction step rather than duplicating the transfer function
|
|
11
|
+
# - Maintains alpha channel transparency values through every stage of the conversion
|
|
12
|
+
# - Uses AbcDecimal arithmetic for precise color science calculations
|
|
13
|
+
#
|
|
14
|
+
# CIELAB is the color model used by the CSS lab() function, and this converter provides the
|
|
15
|
+
# direct path to display-ready sRGB values that the parser and output layers need.
|
|
16
|
+
|
|
17
|
+
require_relative "lab_to_xyz"
|
|
18
|
+
require_relative "xyz_to_lrgb"
|
|
19
|
+
require_relative "lrgb_to_srgb"
|
|
20
|
+
|
|
21
|
+
module Abachrome
|
|
22
|
+
module Converters
|
|
23
|
+
class LabToSrgb < Abachrome::Converters::Base
|
|
24
|
+
# Converts a color from CIELAB color space to sRGB color space.
|
|
25
|
+
#
|
|
26
|
+
# This is done by first converting from CIELAB to XYZ, then from XYZ to linear RGB,
|
|
27
|
+
# and finally from linear RGB to sRGB.
|
|
28
|
+
#
|
|
29
|
+
# @param lab_color [Abachrome::Color] Color in CIELAB color space
|
|
30
|
+
# @return [Abachrome::Color] The converted color in sRGB color space
|
|
31
|
+
def self.convert(lab_color)
|
|
32
|
+
LrgbToSrgb.convert(XyzToLrgb.convert(LabToXyz.convert(lab_color)))
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# Abachrome::Converters::LabToXyz - CIELAB to XYZ color space converter
|
|
2
|
+
#
|
|
3
|
+
# This converter transforms colors from the CIELAB (L*a*b*) color space to the CIE 1931 XYZ
|
|
4
|
+
# color space by inverting the CIELAB transfer function and scaling the result by the
|
|
5
|
+
# reference white point. The inverse transfer function applies a cube for values above the
|
|
6
|
+
# CIE epsilon threshold and a linear segment below it, which keeps the transform well
|
|
7
|
+
# behaved near black.
|
|
8
|
+
#
|
|
9
|
+
# Key features:
|
|
10
|
+
# - Implements the standard CIELAB to XYZ transformation with the CIE epsilon threshold
|
|
11
|
+
# - Sources the D65 reference white point from the Illuminants registry
|
|
12
|
+
# - Applies the linear fallback segment for dark colors below the epsilon threshold
|
|
13
|
+
# - Maintains alpha channel transparency values during conversion
|
|
14
|
+
# - Uses AbcDecimal arithmetic for precise color science calculations
|
|
15
|
+
# - Validates input color space to ensure proper CIELAB source data
|
|
16
|
+
#
|
|
17
|
+
# CIELAB is a perceptually motivated color space used by the CSS lab() function, and XYZ is
|
|
18
|
+
# the device-independent reference space that connects it to the rest of the conversion
|
|
19
|
+
# pipeline.
|
|
20
|
+
|
|
21
|
+
module Abachrome
|
|
22
|
+
module Converters
|
|
23
|
+
class LabToXyz < Abachrome::Converters::Base
|
|
24
|
+
# The CIE standard intent threshold, 216/24389, above which the cubic transfer
|
|
25
|
+
# function is used.
|
|
26
|
+
EPSILON = Rational(216, 24389)
|
|
27
|
+
|
|
28
|
+
# The CIE standard slope constant, 24389/27, used by the linear segment of the
|
|
29
|
+
# transfer function.
|
|
30
|
+
KAPPA = Rational(24389, 27)
|
|
31
|
+
|
|
32
|
+
# Converts a color from CIELAB color space to XYZ color space.
|
|
33
|
+
#
|
|
34
|
+
# @param lab_color [Abachrome::Color] The color in CIELAB color space to convert
|
|
35
|
+
# @return [Abachrome::Color] A new Color object in XYZ color space with the converted coordinates
|
|
36
|
+
# @raise [RuntimeError] If the provided color is not in CIELAB color space
|
|
37
|
+
def self.convert(lab_color)
|
|
38
|
+
raise_unless lab_color, :lab
|
|
39
|
+
|
|
40
|
+
l, a, b = lab_color.coordinates.map { |_| AbcDecimal(_) }
|
|
41
|
+
|
|
42
|
+
fy = (l + AD(16)) / AD(116)
|
|
43
|
+
fx = (a / AD(500)) + fy
|
|
44
|
+
fz = fy - (b / AD(200))
|
|
45
|
+
|
|
46
|
+
wx, wy, wz = Illuminants::D65.white_point.map { |_| AD(_) / AD(100) }
|
|
47
|
+
|
|
48
|
+
x = inverse_transfer(fx) * wx
|
|
49
|
+
y = inverse_transfer(fy) * wy
|
|
50
|
+
z = inverse_transfer(fz) * wz
|
|
51
|
+
|
|
52
|
+
Color.new(ColorSpace.find(:xyz), [x, y, z], lab_color.alpha)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Applies the inverse CIELAB transfer function to a single component.
|
|
56
|
+
#
|
|
57
|
+
# Values whose cube exceeds the CIE epsilon threshold are cubed directly. Values below
|
|
58
|
+
# it fall back to the linear segment, which avoids the near-vertical slope the cubic
|
|
59
|
+
# function has close to zero.
|
|
60
|
+
#
|
|
61
|
+
# @param f [AbcDecimal] The transformed component to invert
|
|
62
|
+
# @return [AbcDecimal] The corresponding linear component, relative to the white point
|
|
63
|
+
def self.inverse_transfer(f)
|
|
64
|
+
cubed = f**3
|
|
65
|
+
return cubed if cubed > AD(EPSILON)
|
|
66
|
+
|
|
67
|
+
((f * AD(116)) - AD(16)) / AD(KAPPA)
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# Abachrome::Converters::LchToLab - CIELCH to CIELAB color space converter
|
|
2
|
+
#
|
|
3
|
+
# This converter transforms colors from the CIELCH color space to the CIELAB color space
|
|
4
|
+
# using cylindrical to rectangular coordinate conversion. The transformation converts the
|
|
5
|
+
# cylindrical coordinates (L, C, h) to rectangular coordinates (L, a, b) where lightness
|
|
6
|
+
# remains unchanged, and the a and b components are calculated from chroma and hue using
|
|
7
|
+
# trigonometric functions (cosine and sine respectively).
|
|
8
|
+
#
|
|
9
|
+
# Key features:
|
|
10
|
+
# - Converts CIELCH cylindrical coordinates to CIELAB rectangular coordinates
|
|
11
|
+
# - Preserves lightness component unchanged during conversion
|
|
12
|
+
# - Calculates a component as chroma × cos(hue) for green-red axis positioning
|
|
13
|
+
# - Calculates b component as chroma × sin(hue) for blue-yellow axis positioning
|
|
14
|
+
# - Converts hue angle from degrees to radians for trigonometric calculations
|
|
15
|
+
# - Maintains alpha channel transparency values during conversion
|
|
16
|
+
# - Uses AbcDecimal arithmetic for precise color science calculations
|
|
17
|
+
# - Validates input color space to ensure proper CIELCH source data
|
|
18
|
+
#
|
|
19
|
+
# CIELCH is the cylindrical form of CIELAB used by the CSS lch() function, expressing color
|
|
20
|
+
# in terms of chroma and hue angle rather than opponent axes, which makes hue rotations and
|
|
21
|
+
# saturation adjustments straightforward.
|
|
22
|
+
|
|
23
|
+
module Abachrome
|
|
24
|
+
module Converters
|
|
25
|
+
class LchToLab < Abachrome::Converters::Base
|
|
26
|
+
# Converts a color from CIELCH color space to CIELAB color space.
|
|
27
|
+
#
|
|
28
|
+
# @param lch_color [Abachrome::Color] The color in CIELCH color space to convert
|
|
29
|
+
# @return [Abachrome::Color] A new Color object in CIELAB color space with the converted coordinates
|
|
30
|
+
# @raise [RuntimeError] If the provided color is not in CIELCH color space
|
|
31
|
+
def self.convert(lch_color)
|
|
32
|
+
raise_unless lch_color, :lch
|
|
33
|
+
|
|
34
|
+
l, c, h = lch_color.coordinates.map { |_| AbcDecimal(_) }
|
|
35
|
+
|
|
36
|
+
h_rad = (h * Math::PI) / AD(180)
|
|
37
|
+
a = c * AD(Math.cos(h_rad.value))
|
|
38
|
+
b = c * AD(Math.sin(h_rad.value))
|
|
39
|
+
|
|
40
|
+
Color.new(
|
|
41
|
+
ColorSpace.find(:lab),
|
|
42
|
+
[l, a, b],
|
|
43
|
+
lch_color.alpha
|
|
44
|
+
)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# Abachrome::Converters::LchToSrgb - CIELCH to sRGB color space converter
|
|
2
|
+
#
|
|
3
|
+
# This converter transforms colors from the CIELCH color space to the standard RGB (sRGB)
|
|
4
|
+
# color space by first converting the cylindrical CIELCH coordinates to rectangular CIELAB
|
|
5
|
+
# coordinates, then routing through the CIELAB to sRGB conversion path.
|
|
6
|
+
#
|
|
7
|
+
# Key features:
|
|
8
|
+
# - Composes the CIELCH to CIELAB and CIELAB to sRGB conversions
|
|
9
|
+
# - Maintains alpha channel transparency values through every stage of the conversion
|
|
10
|
+
# - Uses AbcDecimal arithmetic for precise color science calculations
|
|
11
|
+
#
|
|
12
|
+
# CIELCH is the color model used by the CSS lch() function, and this converter provides the
|
|
13
|
+
# direct path to display-ready sRGB values that the parser and output layers need.
|
|
14
|
+
|
|
15
|
+
require_relative "lch_to_lab"
|
|
16
|
+
require_relative "lab_to_srgb"
|
|
17
|
+
|
|
18
|
+
module Abachrome
|
|
19
|
+
module Converters
|
|
20
|
+
class LchToSrgb < Abachrome::Converters::Base
|
|
21
|
+
# Converts a color from CIELCH color space to sRGB color space.
|
|
22
|
+
#
|
|
23
|
+
# This is done by first converting from CIELCH to CIELAB, then from CIELAB to sRGB.
|
|
24
|
+
#
|
|
25
|
+
# @param lch_color [Abachrome::Color] Color in CIELCH color space
|
|
26
|
+
# @return [Abachrome::Color] The converted color in sRGB color space
|
|
27
|
+
def self.convert(lch_color)
|
|
28
|
+
LabToSrgb.convert(LchToLab.convert(lch_color))
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Abachrome
|
|
4
|
+
module Converters
|
|
5
|
+
class LmsToOklab < Abachrome::Converters::Base
|
|
6
|
+
# Converts a color from LMS color space to OKLAB color space.
|
|
7
|
+
#
|
|
8
|
+
# This method converts LMS to LRGB first, then to OKLAB. The LMS color space
|
|
9
|
+
# represents the response of the three types of cone cells in the human eye.
|
|
10
|
+
#
|
|
11
|
+
# @param lms_color [Abachrome::Color] The color in LMS color space
|
|
12
|
+
# @raise [ArgumentError] If the input color is not in LMS color space
|
|
13
|
+
# @return [Abachrome::Color] The resulting color in OKLAB color space with
|
|
14
|
+
# the same alpha as the input color
|
|
15
|
+
def self.convert(lms_color)
|
|
16
|
+
raise_unless lms_color, :lms
|
|
17
|
+
|
|
18
|
+
# Convert LMS to LRGB first
|
|
19
|
+
lrgb_color = Converters::LmsToLrgb.convert(lms_color)
|
|
20
|
+
|
|
21
|
+
# Then convert LRGB to OKLAB
|
|
22
|
+
Converters::LrgbToOklab.convert(lrgb_color)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
@@ -1,3 +1,29 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
module Abachrome
|
|
4
|
+
module Converters
|
|
5
|
+
class LrgbToLms < Abachrome::Converters::Base
|
|
6
|
+
# Converts a color from linear RGB color space to LMS color space.
|
|
7
|
+
#
|
|
8
|
+
# This method converts linear RGB to XYZ first, then to LMS cone response space.
|
|
9
|
+
# The LMS color space represents the response of the three types of cone cells
|
|
10
|
+
# in the human eye (Long, Medium, Short wavelength sensitivity).
|
|
11
|
+
#
|
|
12
|
+
# @param lrgb_color [Abachrome::Color] The color in linear RGB color space
|
|
13
|
+
# @raise [ArgumentError] If the input color is not in linear RGB color space
|
|
14
|
+
# @return [Abachrome::Color] The resulting color in LMS color space with
|
|
15
|
+
# the same alpha as the input color
|
|
16
|
+
def self.convert(lrgb_color)
|
|
17
|
+
raise_unless lrgb_color, :lrgb
|
|
18
|
+
|
|
19
|
+
# Convert linear RGB to XYZ first
|
|
20
|
+
xyz_color = Converters::LrgbToXyz.convert(lrgb_color)
|
|
21
|
+
|
|
22
|
+
# Then convert XYZ to LMS
|
|
23
|
+
Converters::XyzToLms.convert(xyz_color)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
3
29
|
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Abachrome
|
|
4
|
+
module Converters
|
|
5
|
+
class OklabToXyz < Abachrome::Converters::Base
|
|
6
|
+
# Converts a color from OKLAB color space to XYZ color space.
|
|
7
|
+
#
|
|
8
|
+
# This method converts OKLAB to LMS first, then to XYZ. The OKLAB color space
|
|
9
|
+
# is a perceptually uniform color space, and XYZ is the CIE 1931 color space
|
|
10
|
+
# that forms the basis for most other color space definitions.
|
|
11
|
+
#
|
|
12
|
+
# @param oklab_color [Abachrome::Color] The color in OKLAB color space
|
|
13
|
+
# @raise [ArgumentError] If the input color is not in OKLAB color space
|
|
14
|
+
# @return [Abachrome::Color] The resulting color in XYZ color space with
|
|
15
|
+
# the same alpha as the input color
|
|
16
|
+
def self.convert(oklab_color)
|
|
17
|
+
raise_unless oklab_color, :oklab
|
|
18
|
+
|
|
19
|
+
# Convert OKLAB to LMS first
|
|
20
|
+
lms_color = Converters::OklabToLms.convert(oklab_color)
|
|
21
|
+
|
|
22
|
+
# Then convert LMS to XYZ
|
|
23
|
+
Converters::LmsToXyz.convert(lms_color)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Abachrome
|
|
4
|
+
module Converters
|
|
5
|
+
class OklchToLms < Abachrome::Converters::Base
|
|
6
|
+
# Converts a color from OKLCH color space to LMS color space.
|
|
7
|
+
#
|
|
8
|
+
# This method converts OKLCH to OKLAB first, then to LMS. The OKLCH color space
|
|
9
|
+
# is a cylindrical representation of OKLAB using lightness, chroma, and hue.
|
|
10
|
+
#
|
|
11
|
+
# @param oklch_color [Abachrome::Color] The color in OKLCH color space
|
|
12
|
+
# @raise [ArgumentError] If the input color is not in OKLCH color space
|
|
13
|
+
# @return [Abachrome::Color] The resulting color in LMS color space with
|
|
14
|
+
# the same alpha as the input color
|
|
15
|
+
def self.convert(oklch_color)
|
|
16
|
+
raise_unless oklch_color, :oklch
|
|
17
|
+
|
|
18
|
+
# Convert OKLCH to OKLAB first
|
|
19
|
+
oklab_color = Converters::OklchToOklab.convert(oklch_color)
|
|
20
|
+
|
|
21
|
+
# Then convert OKLAB to LMS
|
|
22
|
+
Converters::OklabToLms.convert(oklab_color)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Abachrome
|
|
4
|
+
module Converters
|
|
5
|
+
class SrgbToLms < Abachrome::Converters::Base
|
|
6
|
+
# Converts a color from sRGB color space to LMS color space.
|
|
7
|
+
#
|
|
8
|
+
# This method converts sRGB to linear RGB first, then to LMS cone response space.
|
|
9
|
+
# The LMS color space represents the response of the three types of cone cells
|
|
10
|
+
# in the human eye (Long, Medium, Short wavelength sensitivity).
|
|
11
|
+
#
|
|
12
|
+
# @param srgb_color [Abachrome::Color] The color in sRGB color space
|
|
13
|
+
# @raise [ArgumentError] If the input color is not in sRGB color space
|
|
14
|
+
# @return [Abachrome::Color] The resulting color in LMS color space with
|
|
15
|
+
# the same alpha as the input color
|
|
16
|
+
def self.convert(srgb_color)
|
|
17
|
+
raise_unless srgb_color, :srgb
|
|
18
|
+
|
|
19
|
+
# Convert sRGB to linear RGB first
|
|
20
|
+
lrgb_color = Converters::SrgbToLrgb.convert(srgb_color)
|
|
21
|
+
|
|
22
|
+
# Then convert linear RGB to LMS
|
|
23
|
+
Converters::LrgbToLms.convert(lrgb_color)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Abachrome::Converters::SrgbToXyz - sRGB to XYZ color space converter
|
|
4
|
+
#
|
|
5
|
+
# This converter transforms colors from the standard RGB (sRGB) color space to the CIE 1931
|
|
6
|
+
# XYZ color space by routing through the intermediate linear RGB color space. The sRGB
|
|
7
|
+
# values are first linearized by removing the gamma correction, then transformed into XYZ
|
|
8
|
+
# through the sRGB primaries matrix with a D65 white point.
|
|
9
|
+
#
|
|
10
|
+
# Key features:
|
|
11
|
+
# - Composes the sRGB to linear RGB and linear RGB to XYZ conversions
|
|
12
|
+
# - Reuses the shared gamma decoding step rather than duplicating the transfer function
|
|
13
|
+
# - Maintains alpha channel transparency values through every stage of the conversion
|
|
14
|
+
# - Uses AbcDecimal arithmetic for precise color science calculations
|
|
15
|
+
#
|
|
16
|
+
# XYZ is the device-independent reference space that connects sRGB to the CIELAB and CIELCH
|
|
17
|
+
# conversion paths, making this the entry point for converting display colors into those
|
|
18
|
+
# perceptually motivated spaces.
|
|
19
|
+
|
|
20
|
+
require_relative "srgb_to_lrgb"
|
|
21
|
+
require_relative "lrgb_to_xyz"
|
|
22
|
+
|
|
23
|
+
module Abachrome
|
|
24
|
+
module Converters
|
|
25
|
+
class SrgbToXyz < Abachrome::Converters::Base
|
|
26
|
+
# Converts a color from sRGB color space to XYZ color space.
|
|
27
|
+
#
|
|
28
|
+
# This is done by first converting from sRGB to linear RGB by removing gamma
|
|
29
|
+
# correction, then applying the linear RGB to XYZ transformation matrix.
|
|
30
|
+
#
|
|
31
|
+
# @param srgb_color [Abachrome::Color] Color in sRGB color space
|
|
32
|
+
# @raise [RuntimeError] If the provided color is not in sRGB color space
|
|
33
|
+
# @return [Abachrome::Color] The converted color in XYZ color space with the
|
|
34
|
+
# same alpha as the input color
|
|
35
|
+
def self.convert(srgb_color)
|
|
36
|
+
raise_unless srgb_color, :srgb
|
|
37
|
+
|
|
38
|
+
LrgbToXyz.convert(SrgbToLrgb.convert(srgb_color))
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Abachrome::Converters::XyzToLrgb - XYZ to linear RGB color space converter
|
|
4
|
+
#
|
|
5
|
+
# This converter transforms colors from the CIE 1931 XYZ color space to the linear RGB
|
|
6
|
+
# (LRGB) color space using the standard inverse transformation matrix for sRGB primaries
|
|
7
|
+
# with a D65 white point. The result is linear light, so it is the natural input to the
|
|
8
|
+
# existing linear RGB to sRGB gamma correction step.
|
|
9
|
+
#
|
|
10
|
+
# Key features:
|
|
11
|
+
# - Implements the inverse of the linear RGB to XYZ transformation matrix (sRGB/D65)
|
|
12
|
+
# - Produces linear light values suitable for subsequent gamma correction
|
|
13
|
+
# - Maintains alpha channel transparency values during conversion
|
|
14
|
+
# - Uses AbcDecimal arithmetic for precise color science calculations
|
|
15
|
+
# - Validates input color space to ensure proper XYZ source data
|
|
16
|
+
#
|
|
17
|
+
# This converter closes the loop with LrgbToXyz, allowing colors that arrive through the
|
|
18
|
+
# CIELAB and CIELCH conversion paths to reach sRGB through the same gamma correction used
|
|
19
|
+
# by every other conversion in the library.
|
|
20
|
+
|
|
21
|
+
module Abachrome
|
|
22
|
+
module Converters
|
|
23
|
+
class XyzToLrgb < Abachrome::Converters::Base
|
|
24
|
+
# Converts a color from XYZ color space to linear RGB color space.
|
|
25
|
+
#
|
|
26
|
+
# @param xyz_color [Abachrome::Color] The color in XYZ color space to convert
|
|
27
|
+
# @return [Abachrome::Color] A new Color object in linear RGB color space with the converted coordinates
|
|
28
|
+
# @raise [RuntimeError] If the provided color is not in XYZ color space
|
|
29
|
+
def self.convert(xyz_color)
|
|
30
|
+
raise_unless xyz_color, :xyz
|
|
31
|
+
|
|
32
|
+
x, y, z = xyz_color.coordinates.map { |_| AbcDecimal(_) }
|
|
33
|
+
|
|
34
|
+
# XYZ to linear RGB transformation matrix (sRGB/D65), the inverse of the
|
|
35
|
+
# matrix used by LrgbToXyz.
|
|
36
|
+
r = (x * AD("3.2404542")) - (y * AD("1.5371385")) - (z * AD("0.4985314"))
|
|
37
|
+
g = (x * AD("-0.9692660")) + (y * AD("1.8760108")) + (z * AD("0.0415560"))
|
|
38
|
+
b = (x * AD("0.0556434")) - (y * AD("0.2040259")) + (z * AD("1.0572252"))
|
|
39
|
+
|
|
40
|
+
Color.new(ColorSpace.find(:lrgb), [r, g, b], xyz_color.alpha)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Abachrome
|
|
4
|
+
module Converters
|
|
5
|
+
class XyzToSrgb < Abachrome::Converters::Base
|
|
6
|
+
# Converts a color from XYZ color space to sRGB color space.
|
|
7
|
+
#
|
|
8
|
+
# This method first converts XYZ to linear RGB using the inverse transformation
|
|
9
|
+
# matrix, then applies gamma correction to convert to sRGB. The XYZ color space
|
|
10
|
+
# is the CIE 1931 color space that forms the basis for most other color space
|
|
11
|
+
# definitions and serves as a device-independent reference.
|
|
12
|
+
#
|
|
13
|
+
# @param xyz_color [Abachrome::Color] The color in XYZ color space
|
|
14
|
+
# @raise [ArgumentError] If the input color is not in XYZ color space
|
|
15
|
+
# @return [Abachrome::Color] The resulting color in sRGB color space with
|
|
16
|
+
# the same alpha as the input color
|
|
17
|
+
def self.convert(xyz_color)
|
|
18
|
+
raise_unless xyz_color, :xyz
|
|
19
|
+
|
|
20
|
+
# Convert XYZ to linear RGB first
|
|
21
|
+
lrgb_color = Converters::XyzToLrgb.convert(xyz_color)
|
|
22
|
+
|
|
23
|
+
# Then convert linear RGB to sRGB
|
|
24
|
+
Converters::LrgbToSrgb.convert(lrgb_color)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
|