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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 584251d0988a5611a22766c4665f4be880d636c70af0a612d7eb39994e32e1cc
4
- data.tar.gz: 0c1b82ff4874f2c74d494cdc79b5b2b4c8259c7e387441477a3a6dcb2046faad
3
+ metadata.gz: aab3cdb1bc8e3a201dda4f6170db0db6c624399a27e22c7a6fac7bb4d5454b8b
4
+ data.tar.gz: 3c42d3dadc6e0d0994b71ea9410a0eff636d37ebcc79763880ca758c042abf6c
5
5
  SHA512:
6
- metadata.gz: 10cd1fd7eb789c3e3fb0d10a1753a139ef83c388d5d22b850397995872784cf8cc2a134d7f423eb8365e609919d2814c1decd16abf9ea7941e2718f587352a46
7
- data.tar.gz: a0dac04fc6b12902df2650b50794f044467c8521f352123239f862ec547970601a623d04306ab4c124af96e64cd317a77cf1781302d31d54e54d9a005c4dcce9
6
+ metadata.gz: d61cb2c127a1e698c1ec0201d055bd4a777741ccebc4857bc2f9fdf75c91d33712fb266b786271a861c791f4525cd13c6c948e8375f5091f321ef0e3dc3bc60c
7
+ data.tar.gz: 4edc57e63a6e6aae86912c81783164c037758452b6c9c717a3faed1fe569ee9b201532e2c9ff10ad3ab522be1895a60d1083ce2505f006e8fc9eb45124a87e0d
data/README.md CHANGED
@@ -14,6 +14,10 @@ Abachrome is a Ruby gem for parsing, manipulating, and managing colors. It provi
14
14
 
15
15
  ![Abachrome Logo](logo.png)
16
16
 
17
+ ## Getting Help
18
+
19
+ Questions about Abachrome? [Practical Ruby Gems](https://practicalrubygems.com/gems/abachrome/) is a good place to get them answered.
20
+
17
21
  ## Installation
18
22
 
19
23
  Add this line to your application's Gemfile:
@@ -320,6 +320,10 @@ module Abachrome
320
320
  @value.negative?
321
321
  end
322
322
 
323
+ def positive?
324
+ @value > 0
325
+ end
326
+
323
327
  # Calculates the arctangent of y/x using the signs of the arguments to determine the quadrant.
324
328
  # Unlike the standard Math.atan2, this method accepts AbcDecimal objects or any values
325
329
  # that can be converted to AbcDecimal.
@@ -76,6 +76,54 @@ module Abachrome
76
76
  new(space, [r, g, b], a)
77
77
  end
78
78
 
79
+ # Creates a new Color instance from HSL values
80
+ #
81
+ # @param h [Numeric] The hue angle in degrees (typically 0-360)
82
+ # @param s [Numeric] The saturation component value (typically 0-1)
83
+ # @param l [Numeric] The lightness component value (typically 0-1)
84
+ # @param a [Numeric] The alpha (opacity) component value (0-1), defaults to 1.0 (fully opaque)
85
+ # @return [Abachrome::Color] A new Color instance in the HSL color space
86
+ def self.from_hsl(h, s, l, a = 1.0)
87
+ space = ColorSpace.find(:hsl)
88
+ new(space, [h, s, l], a)
89
+ end
90
+
91
+ # Creates a new Color instance from HWB values
92
+ #
93
+ # @param h [Numeric] The hue angle in degrees (typically 0-360)
94
+ # @param w [Numeric] The whiteness component value (typically 0-1)
95
+ # @param b [Numeric] The blackness component value (typically 0-1)
96
+ # @param a [Numeric] The alpha (opacity) component value (0-1), defaults to 1.0 (fully opaque)
97
+ # @return [Abachrome::Color] A new Color instance in the HWB color space
98
+ def self.from_hwb(h, w, b, a = 1.0)
99
+ space = ColorSpace.find(:hwb)
100
+ new(space, [h, w, b], a)
101
+ end
102
+
103
+ # Creates a new Color instance from CIELAB values
104
+ #
105
+ # @param l [Numeric] The lightness component (L*), typically in range 0-100
106
+ # @param a [Numeric] The green-red component (a*) of the CIELAB color space
107
+ # @param b [Numeric] The blue-yellow component (b*) of the CIELAB color space
108
+ # @param alpha [Numeric] The alpha (opacity) value, from 0.0 to 1.0
109
+ # @return [Abachrome::Color] A new Color instance in the CIELAB color space
110
+ def self.from_lab(l, a, b, alpha = 1.0)
111
+ space = ColorSpace.find(:lab)
112
+ new(space, [l, a, b], alpha)
113
+ end
114
+
115
+ # Creates a new Color instance from CIELCH values
116
+ #
117
+ # @param l [Numeric] The lightness component (L*), typically in range 0-100
118
+ # @param c [Numeric] The chroma component (C*), typically starting from 0 with no upper bound
119
+ # @param h [Numeric] The hue angle in degrees, typically in range 0-360
120
+ # @param alpha [Numeric] The alpha (opacity) value, from 0.0 to 1.0
121
+ # @return [Abachrome::Color] A new Color instance in the CIELCH color space
122
+ def self.from_lch(l, c, h, alpha = 1.0)
123
+ space = ColorSpace.find(:lch)
124
+ new(space, [l, c, h], alpha)
125
+ end
126
+
79
127
  # Creates a new Color object with OKLAB values.
80
128
  #
81
129
  # @param l [Float] The lightness component (L) of the OKLAB color space
@@ -125,6 +173,30 @@ module Abachrome
125
173
  new(space, [c, m, y, k], alpha)
126
174
  end
127
175
 
176
+ # Creates a new Color instance from XYZ values
177
+ #
178
+ # @param x [Numeric] The X tristimulus value representing the CIE RGB red primary
179
+ # @param y [Numeric] The Y tristimulus value representing luminance
180
+ # @param z [Numeric] The Z tristimulus value representing the CIE RGB blue primary
181
+ # @param alpha [Numeric] The alpha (opacity) component value (0-1), defaults to 1.0 (fully opaque)
182
+ # @return [Abachrome::Color] A new Color instance in the XYZ color space
183
+ def self.from_xyz(x, y, z, alpha = 1.0)
184
+ space = ColorSpace.find(:xyz)
185
+ new(space, [x, y, z], alpha)
186
+ end
187
+
188
+ # Creates a new Color instance from LMS values
189
+ #
190
+ # @param l [Numeric] The long wavelength (L) cone response component
191
+ # @param m [Numeric] The medium wavelength (M) cone response component
192
+ # @param s [Numeric] The short wavelength (S) cone response component
193
+ # @param alpha [Numeric] The alpha (opacity) component value (0-1), defaults to 1.0 (fully opaque)
194
+ # @return [Abachrome::Color] A new Color instance in the LMS color space
195
+ def self.from_lms(l, m, s, alpha = 1.0)
196
+ space = ColorSpace.find(:lms)
197
+ new(space, [l, m, s], alpha)
198
+ end
199
+
128
200
  # Compares this color instance with another for equality.
129
201
  #
130
202
  # Two colors are considered equal if they have the same color space,
@@ -0,0 +1,187 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Abachrome::ColorMixins::Harmonies - Color Harmony Generation Module
4
+ #
5
+ # This module provides methods for generating color harmonies based on color theory.
6
+ # Color harmonies are sets of colors that work well together according to established
7
+ # design principles. The module uses OKLCH color space for hue manipulation, which
8
+ # provides perceptually uniform results.
9
+ #
10
+ # Key features:
11
+ # - Analogous harmony: Colors adjacent on the color wheel (±30°)
12
+ # - Complementary harmony: Colors opposite on the color wheel (180°)
13
+ # - Triadic harmony: Three colors evenly spaced around the color wheel (120° intervals)
14
+ # - Tetradic/Square harmony: Four colors evenly spaced (90° intervals)
15
+ # - Split-complementary harmony: Base color plus two colors adjacent to its complement (150°, 210°)
16
+ # - Option to generate harmonies in different color spaces (HSL or OKLCH)
17
+ #
18
+ # All harmonies preserve the lightness and chroma of the base color while rotating
19
+ # the hue to create harmonious color combinations.
20
+ #
21
+ # References:
22
+ # - Color Theory: https://en.wikipedia.org/wiki/Color_theory
23
+ # - Color Harmony: https://en.wikipedia.org/wiki/Harmony_(color)
24
+
25
+ module Abachrome
26
+ module ColorMixins
27
+ module Harmonies
28
+ # Generates an analogous color harmony.
29
+ # Analogous colors are adjacent to each other on the color wheel,
30
+ # typically within ±30 degrees. This creates a harmonious, cohesive palette.
31
+ #
32
+ # @param angle [Numeric] The hue angle offset in degrees (default: 30)
33
+ # @param space [Symbol] The color space to use for hue manipulation (:hsl or :oklch, default: :oklch)
34
+ # @return [Array<Abachrome::Color>] An array of three colors: [color at -angle, original, color at +angle]
35
+ def analogous(angle: 30, space: :oklch)
36
+ original_space = color_space
37
+
38
+ # Convert to the specified space for hue manipulation
39
+ color_in_space = space == color_space.id ? self : to_color_space(space)
40
+ l, c, h = color_in_space.coordinates
41
+
42
+ # Generate analogous colors by rotating hue
43
+ color_minus = create_harmony_color(l, c, h - angle, space)
44
+ color_plus = create_harmony_color(l, c, h + angle, space)
45
+
46
+ # Convert back to original color space
47
+ [
48
+ color_minus.to_color_space(original_space.id),
49
+ self,
50
+ color_plus.to_color_space(original_space.id)
51
+ ]
52
+ end
53
+
54
+ # Generates a complementary color harmony.
55
+ # Complementary colors are opposite each other on the color wheel (180° apart).
56
+ # They create maximum contrast and vibrant combinations.
57
+ #
58
+ # @param space [Symbol] The color space to use for hue manipulation (:hsl or :oklch, default: :oklch)
59
+ # @return [Array<Abachrome::Color>] An array of two colors: [original, complement]
60
+ def complementary(space: :oklch)
61
+ original_space = color_space
62
+
63
+ # Convert to the specified space for hue manipulation
64
+ color_in_space = space == color_space.id ? self : to_color_space(space)
65
+ l, c, h = color_in_space.coordinates
66
+
67
+ # Generate complementary color by rotating hue 180°
68
+ complement = create_harmony_color(l, c, h + 180, space)
69
+
70
+ # Convert back to original color space
71
+ [
72
+ self,
73
+ complement.to_color_space(original_space.id)
74
+ ]
75
+ end
76
+
77
+ # Generates a triadic color harmony.
78
+ # Triadic colors are evenly spaced around the color wheel at 120° intervals.
79
+ # They create vibrant, balanced palettes with good contrast.
80
+ #
81
+ # @param space [Symbol] The color space to use for hue manipulation (:hsl or :oklch, default: :oklch)
82
+ # @return [Array<Abachrome::Color>] An array of three colors evenly spaced around the color wheel
83
+ def triadic(space: :oklch)
84
+ original_space = color_space
85
+
86
+ # Convert to the specified space for hue manipulation
87
+ color_in_space = space == color_space.id ? self : to_color_space(space)
88
+ l, c, h = color_in_space.coordinates
89
+
90
+ # Generate triadic colors by rotating hue 120° and 240°
91
+ color_1 = create_harmony_color(l, c, h + 120, space)
92
+ color_2 = create_harmony_color(l, c, h + 240, space)
93
+
94
+ # Convert back to original color space
95
+ [
96
+ self,
97
+ color_1.to_color_space(original_space.id),
98
+ color_2.to_color_space(original_space.id)
99
+ ]
100
+ end
101
+
102
+ # Generates a tetradic (square) color harmony.
103
+ # Tetradic colors are evenly spaced around the color wheel at 90° intervals.
104
+ # They create rich, varied palettes with multiple complementary pairs.
105
+ #
106
+ # @param space [Symbol] The color space to use for hue manipulation (:hsl or :oklch, default: :oklch)
107
+ # @return [Array<Abachrome::Color>] An array of four colors evenly spaced around the color wheel
108
+ def tetradic(space: :oklch)
109
+ original_space = color_space
110
+
111
+ # Convert to the specified space for hue manipulation
112
+ color_in_space = space == color_space.id ? self : to_color_space(space)
113
+ l, c, h = color_in_space.coordinates
114
+
115
+ # Generate tetradic colors by rotating hue 90°, 180°, and 270°
116
+ color_1 = create_harmony_color(l, c, h + 90, space)
117
+ color_2 = create_harmony_color(l, c, h + 180, space)
118
+ color_3 = create_harmony_color(l, c, h + 270, space)
119
+
120
+ # Convert back to original color space
121
+ [
122
+ self,
123
+ color_1.to_color_space(original_space.id),
124
+ color_2.to_color_space(original_space.id),
125
+ color_3.to_color_space(original_space.id)
126
+ ]
127
+ end
128
+
129
+ # Generates a split-complementary color harmony.
130
+ # Split-complementary uses the base color plus two colors adjacent to its complement
131
+ # (at 150° and 210° from the base). This creates strong visual contrast while being
132
+ # more subtle than a pure complementary scheme.
133
+ #
134
+ # @param space [Symbol] The color space to use for hue manipulation (:hsl or :oklch, default: :oklch)
135
+ # @return [Array<Abachrome::Color>] An array of three colors: [original, complement-30°, complement+30°]
136
+ def split_complementary(space: :oklch)
137
+ original_space = color_space
138
+
139
+ # Convert to the specified space for hue manipulation
140
+ color_in_space = space == color_space.id ? self : to_color_space(space)
141
+ l, c, h = color_in_space.coordinates
142
+
143
+ # Generate split-complementary colors at 150° and 210° (complement ± 30°)
144
+ color_1 = create_harmony_color(l, c, h + 150, space)
145
+ color_2 = create_harmony_color(l, c, h + 210, space)
146
+
147
+ # Convert back to original color space
148
+ [
149
+ self,
150
+ color_1.to_color_space(original_space.id),
151
+ color_2.to_color_space(original_space.id)
152
+ ]
153
+ end
154
+
155
+ private
156
+
157
+ # Helper method to create a harmony color with normalized hue
158
+ #
159
+ # @param l [AbcDecimal] The lightness value
160
+ # @param c [AbcDecimal] The chroma value
161
+ # @param h [Numeric] The hue angle in degrees (will be normalized to 0-360)
162
+ # @param space [Symbol] The color space (:hsl or :oklch)
163
+ # @return [Abachrome::Color] A new color in the specified color space
164
+ def create_harmony_color(l, c, h, space)
165
+ # Normalize hue to 0-360 range
166
+ normalized_hue = h % 360
167
+
168
+ case space
169
+ when :oklch
170
+ Abachrome::Color.from_oklch(l, c, normalized_hue, alpha)
171
+ when :hsl
172
+ # For HSL, assuming coordinates are [h, s, l]
173
+ # Note: HSL hue is first coordinate
174
+ Abachrome::Color.new(
175
+ Abachrome::ColorSpace.find(:hsl),
176
+ [normalized_hue, c, l],
177
+ alpha
178
+ )
179
+ else
180
+ raise ArgumentError, "Unsupported color space for harmonies: #{space}. Use :oklch or :hsl"
181
+ end
182
+ end
183
+ end
184
+ end
185
+ end
186
+
187
+ # Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Abachrome::ColorMixins::ToLms - LMS color space conversion functionality
4
+ #
5
+ # This mixin provides methods for converting colors to the LMS color space, which represents
6
+ # the response of the three types of cone cells in the human eye (Long, Medium, Short wavelength
7
+ # sensitivity). LMS serves as an intermediate color space in the OKLAB transformation pipeline
8
+ # and provides a foundation for perceptually uniform color representations.
9
+ #
10
+ # Key features:
11
+ # - Convert colors to LMS with automatic converter lookup
12
+ # - Both non-destructive (to_lms) and destructive (to_lms!) conversion methods
13
+ # - Direct access to LMS components (long, medium, short)
14
+ # - Utility methods for LMS array and value extraction
15
+ # - Optimized to return the same object when no conversion is needed
16
+ # - High-precision decimal arithmetic for accurate color science calculations
17
+ #
18
+ # The LMS color space uses three components: L (long wavelength), M (medium wavelength),
19
+ # and S (short wavelength), representing cone cell responses that bridge the gap between
20
+ # linear RGB and perceptually uniform color spaces like OKLAB.
21
+
22
+ require_relative "../converter"
23
+
24
+ module Abachrome
25
+ module ColorMixins
26
+ module ToLms
27
+ # Converts the current color to the LMS color space.
28
+ #
29
+ # If the color is already in LMS, it returns the color unchanged.
30
+ # Otherwise, it uses the Converter to transform the color to LMS.
31
+ #
32
+ # @return [Abachrome::Color] A new Color object in the LMS color space
33
+ def to_lms
34
+ return self if color_space.name == :lms
35
+
36
+ Converter.convert(self, :lms)
37
+ end
38
+
39
+ # Converts the color to the LMS color space in place.
40
+ # This method transforms the current color into LMS space,
41
+ # modifying the original object by updating its color space
42
+ # and coordinates if not already in LMS.
43
+ #
44
+ # @example
45
+ # color = Abachrome::Color.from_hex("#ff5500")
46
+ # color.to_lms! # Color now uses LMS color space
47
+ #
48
+ # @return [Abachrome::Color] self, with updated color space and coordinates
49
+ def to_lms!
50
+ unless color_space.name == :lms
51
+ lms_color = to_lms
52
+ @color_space = lms_color.color_space
53
+ @coordinates = lms_color.coordinates
54
+ end
55
+ self
56
+ end
57
+
58
+ # Returns the L component (long wavelength) from the LMS color space.
59
+ #
60
+ # The L component represents the response of long-wavelength sensitive cone cells
61
+ # in the human eye, which are most sensitive to red light.
62
+ #
63
+ # @return [AbcDecimal] The L (long wavelength) value from the LMS color space
64
+ def long
65
+ to_lms.coordinates[0]
66
+ end
67
+
68
+ # Returns the M component (medium wavelength) from the LMS color space.
69
+ #
70
+ # The M component represents the response of medium-wavelength sensitive cone cells
71
+ # in the human eye, which are most sensitive to green light.
72
+ #
73
+ # @return [AbcDecimal] The M (medium wavelength) value from the LMS color space
74
+ def medium
75
+ to_lms.coordinates[1]
76
+ end
77
+
78
+ # Returns the S component (short wavelength) from the LMS color space.
79
+ #
80
+ # The S component represents the response of short-wavelength sensitive cone cells
81
+ # in the human eye, which are most sensitive to blue light.
82
+ #
83
+ # @return [AbcDecimal] The S (short wavelength) value from the LMS color space
84
+ def short
85
+ to_lms.coordinates[2]
86
+ end
87
+
88
+ # Returns the LMS color space coordinates for this color.
89
+ #
90
+ # @return [Array<AbcDecimal>] An array of LMS coordinates [L, M, S] representing the color in LMS color space
91
+ def lms_values
92
+ to_lms.coordinates
93
+ end
94
+
95
+ # Returns an array representation of the color's coordinates in the LMS color space.
96
+ #
97
+ # @return [Array<AbcDecimal>] An array containing the coordinates of the color
98
+ # in the LMS color space in the order [L, M, S]
99
+ def lms_array
100
+ to_lms.coordinates
101
+ end
102
+ end
103
+ end
104
+ end
105
+
106
+ # Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Abachrome::ColorMixins::ToXyz - XYZ color space conversion functionality
4
+ #
5
+ # This mixin provides methods for converting colors to the XYZ color space, which is the
6
+ # CIE 1931 color space that forms the basis for most other color space definitions and
7
+ # serves as a device-independent reference color space. XYZ represents colors using
8
+ # tristimulus values that correspond to the response of the human visual system to light.
9
+ #
10
+ # Key features:
11
+ # - Convert colors to XYZ with automatic converter lookup
12
+ # - Both non-destructive (to_xyz) and destructive (to_xyz!) conversion methods
13
+ # - Direct access to XYZ components (x, y, z)
14
+ # - Utility methods for XYZ array and value extraction
15
+ # - Optimized to return the same object when no conversion is needed
16
+ # - High-precision decimal arithmetic for accurate color science calculations
17
+ #
18
+ # The XYZ color space uses three components: X, Y, and Z tristimulus values, providing
19
+ # a device-independent representation of colors that serves as the foundation for defining
20
+ # other color spaces, making it essential for accurate color transformations.
21
+
22
+ require_relative "../converter"
23
+
24
+ module Abachrome
25
+ module ColorMixins
26
+ module ToXyz
27
+ # Converts the current color to the XYZ color space.
28
+ #
29
+ # If the color is already in XYZ, it returns the color unchanged.
30
+ # Otherwise, it uses the Converter to transform the color to XYZ.
31
+ #
32
+ # @return [Abachrome::Color] A new Color object in the XYZ color space
33
+ def to_xyz
34
+ return self if color_space.name == :xyz
35
+
36
+ Converter.convert(self, :xyz)
37
+ end
38
+
39
+ # Converts the color to the XYZ color space in place.
40
+ # This method transforms the current color into XYZ space,
41
+ # modifying the original object by updating its color space
42
+ # and coordinates if not already in XYZ.
43
+ #
44
+ # @example
45
+ # color = Abachrome::Color.from_hex("#ff5500")
46
+ # color.to_xyz! # Color now uses XYZ color space
47
+ #
48
+ # @return [Abachrome::Color] self, with updated color space and coordinates
49
+ def to_xyz!
50
+ unless color_space.name == :xyz
51
+ xyz_color = to_xyz
52
+ @color_space = xyz_color.color_space
53
+ @coordinates = xyz_color.coordinates
54
+ end
55
+ self
56
+ end
57
+
58
+ # Returns the X component from the XYZ color space.
59
+ #
60
+ # The X component represents the mix of cone response curves chosen to be
61
+ # nonnegative and represents a scale of the CIE RGB red primary.
62
+ #
63
+ # @return [AbcDecimal] The X tristimulus value from the XYZ color space
64
+ def x
65
+ to_xyz.coordinates[0]
66
+ end
67
+
68
+ # Returns the Y component from the XYZ color space.
69
+ #
70
+ # The Y component represents luminance, which closely matches human perception
71
+ # of brightness. It corresponds to the CIE RGB green primary.
72
+ #
73
+ # @return [AbcDecimal] The Y tristimulus value from the XYZ color space
74
+ def y
75
+ to_xyz.coordinates[1]
76
+ end
77
+
78
+ # Returns the Z component from the XYZ color space.
79
+ #
80
+ # The Z component represents the CIE RGB blue primary and is roughly equal
81
+ # to blue stimulation.
82
+ #
83
+ # @return [AbcDecimal] The Z tristimulus value from the XYZ color space
84
+ def z
85
+ to_xyz.coordinates[2]
86
+ end
87
+
88
+ # Returns the XYZ color space coordinates for this color.
89
+ #
90
+ # @return [Array<AbcDecimal>] An array of XYZ coordinates [X, Y, Z] representing the color in XYZ color space
91
+ def xyz_values
92
+ to_xyz.coordinates
93
+ end
94
+
95
+ # Returns an array representation of the color's coordinates in the XYZ color space.
96
+ #
97
+ # @return [Array<AbcDecimal>] An array containing the coordinates of the color
98
+ # in the XYZ color space in the order [X, Y, Z]
99
+ def xyz_array
100
+ to_xyz.coordinates
101
+ end
102
+ end
103
+ end
104
+ end
105
+
106
+ # Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
@@ -0,0 +1,126 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Abachrome::ColorMixins::Wcag - WCAG Accessibility Module
4
+ #
5
+ # This module provides methods for calculating color contrast ratios and checking
6
+ # WCAG 2.0/2.1 compliance for accessibility. It implements the relative luminance
7
+ # calculation with proper sRGB linearization according to WCAG specifications.
8
+ #
9
+ # Key features:
10
+ # - Calculate relative luminance using WCAG formula: Y = 0.2126R + 0.7152G + 0.0722B
11
+ # - Compute contrast ratio between two colors
12
+ # - Check WCAG 2.0 Level AA and AAA compliance for normal and large text
13
+ # - Check WCAG 2.1 non-text contrast compliance (3:1 minimum)
14
+ # - Predicate methods for easy accessibility checks
15
+ #
16
+ # References:
17
+ # - WCAG 2.0: https://www.w3.org/TR/WCAG20/
18
+ # - WCAG 2.1: https://www.w3.org/TR/WCAG21/
19
+ # - Relative luminance: https://www.w3.org/TR/WCAG20/#relativeluminancedef
20
+ # - Contrast ratio: https://www.w3.org/TR/WCAG20/#contrast-ratiodef
21
+
22
+ module Abachrome
23
+ module ColorMixins
24
+ module Wcag
25
+ # Calculates the relative luminance of the color according to WCAG specifications.
26
+ # Uses the formula: Y = 0.2126R + 0.7152G + 0.0722B
27
+ # where R, G, B are linearized sRGB values.
28
+ #
29
+ # The linearization follows the sRGB specification:
30
+ # - For values <= 0.03928: linear_value = value / 12.92
31
+ # - For values > 0.03928: linear_value = ((value + 0.055) / 1.055) ^ 2.4
32
+ #
33
+ # @return [AbcDecimal] The relative luminance value between 0.0 (darkest) and 1.0 (lightest)
34
+ def relative_luminance
35
+ rgb = to_srgb
36
+ r, g, b = rgb.coordinates
37
+
38
+ # Linearize sRGB values
39
+ r_linear = linearize_srgb_component(r)
40
+ g_linear = linearize_srgb_component(g)
41
+ b_linear = linearize_srgb_component(b)
42
+
43
+ # Apply WCAG luminance coefficients (Rec. 709)
44
+ AbcDecimal("0.2126") * r_linear +
45
+ AbcDecimal("0.7152") * g_linear +
46
+ AbcDecimal("0.0722") * b_linear
47
+ end
48
+
49
+ # Calculates the contrast ratio between this color and another color.
50
+ # The contrast ratio is calculated according to WCAG:
51
+ # (L1 + 0.05) / (L2 + 0.05)
52
+ # where L1 is the relative luminance of the lighter color and
53
+ # L2 is the relative luminance of the darker color.
54
+ #
55
+ # @param other [Abachrome::Color] The color to compare against
56
+ # @return [AbcDecimal] The contrast ratio, ranging from 1:1 (no contrast) to 21:1 (maximum contrast)
57
+ def contrast_ratio(other)
58
+ l1 = relative_luminance
59
+ l2 = other.relative_luminance
60
+
61
+ # Ensure L1 is the lighter color
62
+ l1, l2 = l2, l1 if l1 < l2
63
+
64
+ (l1 + AbcDecimal("0.05")) / (l2 + AbcDecimal("0.05"))
65
+ end
66
+
67
+ # Checks if the contrast ratio with another color meets WCAG 2.0 Level AA standards.
68
+ # AA requirements:
69
+ # - Normal text (< 18pt or < 14pt bold): minimum 4.5:1 contrast ratio
70
+ # - Large text (≥ 18pt or ≥ 14pt bold): minimum 3:1 contrast ratio
71
+ #
72
+ # @param other [Abachrome::Color] The color to compare against
73
+ # @param large_text [Boolean] Whether the text is considered large (default: false)
74
+ # @return [Boolean] true if the contrast meets AA standards
75
+ def meets_wcag_aa?(other, large_text: false)
76
+ ratio = contrast_ratio(other)
77
+ minimum = large_text ? AbcDecimal("3.0") : AbcDecimal("4.5")
78
+ ratio >= minimum
79
+ end
80
+
81
+ # Checks if the contrast ratio with another color meets WCAG 2.0 Level AAA standards.
82
+ # AAA requirements:
83
+ # - Normal text (< 18pt or < 14pt bold): minimum 7:1 contrast ratio
84
+ # - Large text (≥ 18pt or ≥ 14pt bold): minimum 4.5:1 contrast ratio
85
+ #
86
+ # @param other [Abachrome::Color] The color to compare against
87
+ # @param large_text [Boolean] Whether the text is considered large (default: false)
88
+ # @return [Boolean] true if the contrast meets AAA standards
89
+ def meets_wcag_aaa?(other, large_text: false)
90
+ ratio = contrast_ratio(other)
91
+ minimum = large_text ? AbcDecimal("4.5") : AbcDecimal("7.0")
92
+ ratio >= minimum
93
+ end
94
+
95
+ # Checks if the contrast ratio with another color meets WCAG 2.1 non-text contrast requirements.
96
+ # Non-text contrast applies to:
97
+ # - Graphical objects (icons, graphs, infographics)
98
+ # - User interface components (buttons, form inputs, focus indicators)
99
+ # Requirement: minimum 3:1 contrast ratio
100
+ #
101
+ # @param other [Abachrome::Color] The color to compare against
102
+ # @return [Boolean] true if the contrast meets the 3:1 minimum for non-text content
103
+ def meets_wcag_non_text?(other)
104
+ ratio = contrast_ratio(other)
105
+ ratio >= AbcDecimal("3.0")
106
+ end
107
+
108
+ private
109
+
110
+ # Linearizes a single sRGB component value according to the sRGB specification.
111
+ # This is required before applying the luminance coefficients.
112
+ #
113
+ # @param component [AbcDecimal] The sRGB component value (0.0 to 1.0)
114
+ # @return [AbcDecimal] The linearized component value
115
+ def linearize_srgb_component(component)
116
+ if component <= AbcDecimal("0.03928")
117
+ component / AbcDecimal("12.92")
118
+ else
119
+ ((component + AbcDecimal("0.055")) / AbcDecimal("1.055"))**AbcDecimal("2.4")
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
125
+
126
+ # Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
@@ -38,7 +38,8 @@ module Abachrome
38
38
  case value
39
39
  when String
40
40
  if value.end_with?("%")
41
- AbcDecimal(value.chomp("%")) / AbcDecimal(100)
41
+ value_without_percent = value.chomp("%")
42
+ AbcDecimal(value_without_percent) / AbcDecimal(100)
42
43
  else
43
44
  AbcDecimal(value)
44
45
  end
@@ -28,4 +28,12 @@ module Abachrome
28
28
  end
29
29
  end
30
30
 
31
+ ColorSpace.register(
32
+ :xyz,
33
+ "XYZ",
34
+ %w[x y z],
35
+ nil,
36
+ []
37
+ )
38
+
31
39
  # Copyright (c) 2025 Durable Programming, LLC. All rights reserved.