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.
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  #
4
2
  # Abachrome::Parsers::CSS - CSS color format parser
5
3
  #
@@ -38,40 +36,44 @@ module Abachrome
38
36
  parse_functional_color(input)
39
37
  end
40
38
 
39
+ private
40
+
41
41
  def self.parse_named_color(input)
42
- # Check if input matches a named color
43
- rgb_values = Named::CSS.method(input.to_sym)&.call
42
+ # CSS defines "transparent" as rgba(0, 0, 0, 0)
43
+ return Color.from_rgb(0, 0, 0, 0) if input == "transparent"
44
+
45
+ rgb_values = Named::CSS::ColorNames[input]
44
46
  return nil unless rgb_values
45
47
 
46
48
  # Convert 0-255 RGB values to 0-1 range
47
49
  r, g, b = rgb_values.map { |v| v / 255.0 }
48
50
  Color.from_rgb(r, g, b)
49
- rescue NameError
50
- nil
51
51
  end
52
52
 
53
53
  def self.parse_functional_color(input)
54
54
  case input
55
55
  when /^rgb\((.+)\)$/
56
- parse_rgb(::Regexp.last_match(1))
56
+ parse_rgb($1)
57
57
  when /^rgba\((.+)\)$/
58
- parse_rgba(::Regexp.last_match(1))
58
+ parse_rgba($1)
59
59
  when /^hsl\((.+)\)$/
60
- parse_hsl(::Regexp.last_match(1))
60
+ parse_hsl($1)
61
61
  when /^hsla\((.+)\)$/
62
- parse_hsla(::Regexp.last_match(1))
62
+ parse_hsla($1)
63
63
  when /^hwb\((.+)\)$/
64
- parse_hwb(::Regexp.last_match(1))
64
+ parse_hwb($1)
65
65
  when /^lab\((.+)\)$/
66
- parse_lab(::Regexp.last_match(1))
66
+ parse_lab($1)
67
67
  when /^lch\((.+)\)$/
68
- parse_lch(::Regexp.last_match(1))
68
+ parse_lch($1)
69
69
  when /^oklab\((.+)\)$/
70
- parse_oklab(::Regexp.last_match(1))
70
+ parse_oklab($1)
71
71
  when /^oklch\((.+)\)$/
72
- parse_oklch(::Regexp.last_match(1))
72
+ parse_oklch($1)
73
73
  when /^color\((.+)\)$/
74
- parse_color_function(::Regexp.last_match(1))
74
+ parse_color_function($1)
75
+ else
76
+ nil
75
77
  end
76
78
  end
77
79
 
@@ -96,8 +98,7 @@ module Abachrome
96
98
  return nil unless values
97
99
 
98
100
  h, s, l = values
99
- rgb = hsl_to_rgb(h, s, l)
100
- Color.from_rgb(*rgb)
101
+ Color.from_hsl(h, s, l)
101
102
  end
102
103
 
103
104
  def self.parse_hsla(params)
@@ -105,8 +106,7 @@ module Abachrome
105
106
  return nil unless values
106
107
 
107
108
  h, s, l, a = values
108
- rgb = hsl_to_rgb(h, s, l)
109
- Color.from_rgb(*rgb, a)
109
+ Color.from_hsl(h, s, l, a)
110
110
  end
111
111
 
112
112
  def self.parse_hwb(params)
@@ -114,8 +114,7 @@ module Abachrome
114
114
  return nil unless values
115
115
 
116
116
  h, w, b, a = values
117
- rgb = hwb_to_rgb(h, w, b)
118
- Color.from_rgb(*rgb, a)
117
+ Color.from_hwb(h, w, b, a)
119
118
  end
120
119
 
121
120
  def self.parse_lab(params)
@@ -123,10 +122,7 @@ module Abachrome
123
122
  return nil unless values
124
123
 
125
124
  l, a, b = values
126
- # Convert CIELAB to XYZ, then to sRGB
127
- xyz = lab_to_xyz(l, a, b)
128
- rgb = xyz_to_rgb(*xyz)
129
- Color.from_rgb(*rgb)
125
+ Color.from_lab(l, a, b)
130
126
  end
131
127
 
132
128
  def self.parse_lch(params)
@@ -134,11 +130,7 @@ module Abachrome
134
130
  return nil unless values
135
131
 
136
132
  l, c, h = values
137
- # Convert CIELCH to CIELAB, then to XYZ, then to sRGB
138
- lab = lch_to_lab(l, c, h)
139
- xyz = lab_to_xyz(*lab)
140
- rgb = xyz_to_rgb(*xyz)
141
- Color.from_rgb(*rgb)
133
+ Color.from_lch(l, c, h)
142
134
  end
143
135
 
144
136
  def self.parse_oklab(params)
@@ -169,43 +161,39 @@ module Abachrome
169
161
  when "srgb"
170
162
  values = parse_color_values(values_str, 3)
171
163
  return nil unless values
172
-
173
164
  r, g, b = values
174
165
  Color.from_rgb(r, g, b)
175
166
  when "srgb-linear"
176
167
  values = parse_color_values(values_str, 3)
177
168
  return nil unless values
178
-
179
169
  r, g, b = values
180
170
  Color.from_lrgb(r, g, b)
181
171
  when "display-p3"
182
172
  # For now, approximate as sRGB
183
173
  values = parse_color_values(values_str, 3)
184
174
  return nil unless values
185
-
186
175
  r, g, b = values
187
176
  Color.from_rgb(r, g, b)
188
177
  when "a98-rgb"
189
178
  # For now, approximate as sRGB
190
179
  values = parse_color_values(values_str, 3)
191
180
  return nil unless values
192
-
193
181
  r, g, b = values
194
182
  Color.from_rgb(r, g, b)
195
183
  when "prophoto-rgb"
196
184
  # For now, approximate as sRGB
197
185
  values = parse_color_values(values_str, 3)
198
186
  return nil unless values
199
-
200
187
  r, g, b = values
201
188
  Color.from_rgb(r, g, b)
202
189
  when "rec2020"
203
190
  # For now, approximate as sRGB
204
191
  values = parse_color_values(values_str, 3)
205
192
  return nil unless values
206
-
207
193
  r, g, b = values
208
194
  Color.from_rgb(r, g, b)
195
+ else
196
+ nil
209
197
  end
210
198
  end
211
199
 
@@ -226,16 +214,15 @@ module Abachrome
226
214
 
227
215
  parsed = []
228
216
  values.each_with_index do |v, i|
229
- val = if i.zero? # Hue
230
- parse_angle_value(v)
231
-
232
- else # Saturation, Lightness, Alpha
233
- parse_percentage_or_number(v)
234
-
235
- end
236
- return nil unless val
237
-
238
- parsed << val
217
+ if i == 0 # Hue
218
+ val = parse_angle_value(v)
219
+ return nil unless val
220
+ parsed << val
221
+ else # Saturation, Lightness, Alpha
222
+ val = parse_percentage_or_number(v)
223
+ return nil unless val
224
+ parsed << val
225
+ end
239
226
  end
240
227
  parsed
241
228
  end
@@ -258,7 +245,7 @@ module Abachrome
258
245
  values = str.split(/\s+/, expected_count).map(&:strip)
259
246
  return nil unless values.length == expected_count
260
247
 
261
- l = parse_percentage_or_number(values[0])
248
+ l = parse_lightness_value(values[0])
262
249
  a = parse_numeric_value(values[1])
263
250
  b = parse_numeric_value(values[2])
264
251
 
@@ -271,7 +258,7 @@ module Abachrome
271
258
  values = str.split(/\s+/, expected_count).map(&:strip)
272
259
  return nil unless values.length == expected_count
273
260
 
274
- l = parse_percentage_or_number(values[0])
261
+ l = parse_lightness_value(values[0])
275
262
  c = parse_numeric_value(values[1])
276
263
  h = parse_angle_value(values[2])
277
264
 
@@ -309,144 +296,60 @@ module Abachrome
309
296
  def self.parse_numeric_value(str)
310
297
  return nil unless str
311
298
 
312
- if str.end_with?("%")
313
- (str.chomp("%").to_f / 100.0)
299
+ if str.end_with?('%')
300
+ (str.chomp('%').to_f / 100.0)
314
301
  else
315
302
  str.to_f
316
303
  end
317
- rescue StandardError
304
+ rescue
318
305
  nil
319
306
  end
320
307
 
321
- def self.parse_percentage_or_number(str)
308
+ # CIELAB and CIELCH lightness runs from 0 to 100, so a percentage maps onto that
309
+ # range rather than onto 0..1 the way the other percentage components do.
310
+ def self.parse_lightness_value(str)
322
311
  return nil unless str
323
312
 
324
- if str.end_with?("%")
325
- str.chomp("%").to_f / 100.0
313
+ if str.end_with?('%')
314
+ str.chomp('%').to_f
326
315
  else
327
316
  str.to_f
328
317
  end
329
- rescue StandardError
318
+ rescue
330
319
  nil
331
320
  end
332
321
 
333
- def self.parse_angle_value(str)
322
+ def self.parse_percentage_or_number(str)
334
323
  return nil unless str
335
324
 
336
- if str.end_with?("deg")
337
- str.chomp("deg").to_f
338
- elsif str.end_with?("rad")
339
- str.chomp("rad").to_f * 180.0 / Math::PI
340
- elsif str.end_with?("grad")
341
- str.chomp("grad").to_f * 0.9
342
- elsif str.end_with?("turn")
343
- str.chomp("turn").to_f * 360.0
325
+ if str.end_with?('%')
326
+ str.chomp('%').to_f / 100.0
344
327
  else
345
- str.to_f # Assume degrees
328
+ str.to_f
346
329
  end
347
- rescue StandardError
330
+ rescue
348
331
  nil
349
332
  end
350
333
 
351
- # Color space conversion functions
352
-
353
- def self.hsl_to_rgb(h, s, l)
354
- h /= 360.0 # Normalize hue to 0-1
355
-
356
- c = (1 - ((2 * l) - 1).abs) * s
357
- x = c * (1 - (((h * 6) % 2) - 1).abs)
358
- m = l - (c / 2)
359
-
360
- if h < 1.0 / 6
361
- r = c
362
- g = x
363
- b = 0
364
- elsif h < 2.0 / 6
365
- r = x
366
- g = c
367
- b = 0
368
- elsif h < 3.0 / 6
369
- r = 0
370
- g = c
371
- b = x
372
- elsif h < 4.0 / 6
373
- r = 0
374
- g = x
375
- b = c
376
- elsif h < 5.0 / 6
377
- r = x
378
- g = 0
379
- b = c
380
- else
381
- r = c
382
- g = 0
383
- b = x
384
- end
385
-
386
- [r + m, g + m, b + m]
387
- end
388
-
389
- def self.hwb_to_rgb(h, w, b)
390
- # Normalize values
391
- h /= 360.0
334
+ def self.parse_angle_value(str)
335
+ return nil unless str
392
336
 
393
- # Calculate RGB from HSL equivalent
394
- if w + b >= 1
395
- gray = w / (w + b)
396
- [gray, gray, gray]
337
+ if str.end_with?('deg')
338
+ str.chomp('deg').to_f
339
+ elsif str.end_with?('rad')
340
+ str.chomp('rad').to_f * 180.0 / Math::PI
341
+ elsif str.end_with?('grad')
342
+ str.chomp('grad').to_f * 0.9
343
+ elsif str.end_with?('turn')
344
+ str.chomp('turn').to_f * 360.0
397
345
  else
398
- rgb = hsl_to_rgb(h * 360, 1, 0.5)
399
- r, g, b_rgb = rgb
400
-
401
- # Apply whiteness and blackness
402
- r = (r * (1 - w - b)) + w
403
- g = (g * (1 - w - b)) + w
404
- b_rgb = (b_rgb * (1 - w - b)) + w
405
-
406
- [r, g, b_rgb]
407
- end
408
- end
409
-
410
- def self.lab_to_xyz(l, a, b)
411
- # CIELAB to XYZ conversion (D65 white point)
412
- y = (l + 16) / 116
413
- x = (a / 500) + y
414
- z = y - (b / 200)
415
-
416
- x = x**3 > 0.008856 ? x**3 : (x - (16 / 116)) / 7.787
417
- y = y**3 > 0.008856 ? y**3 : (y - (16 / 116)) / 7.787
418
- z = z**3 > 0.008856 ? z**3 : (z - (16 / 116)) / 7.787
419
-
420
- # D65 white point
421
- x *= 0.95047
422
- y *= 1.0
423
- z *= 1.08883
424
-
425
- [x, y, z]
426
- end
427
-
428
- def self.lch_to_lab(l, c, h)
429
- h_rad = h * Math::PI / 180.0
430
- a = c * Math.cos(h_rad)
431
- b = c * Math.sin(h_rad)
432
- [l, a, b]
433
- end
434
-
435
- def self.xyz_to_rgb(x, y, z)
436
- # XYZ to linear RGB
437
- r = (x * 3.2406) + (y * -1.5372) + (z * -0.4986)
438
- g = (x * -0.9689) + (y * 1.8758) + (z * 0.0415)
439
- b = (x * 0.0557) + (y * -0.2040) + (z * 1.0570)
440
-
441
- # Linear RGB to sRGB
442
- [r, g, b].map do |v|
443
- if v > 0.0031308
444
- (1.055 * (v**(1 / 2.4))) - 0.055
445
- else
446
- 12.92 * v
447
- end
346
+ str.to_f # Assume degrees
448
347
  end
348
+ rescue
349
+ nil
449
350
  end
450
351
  end
451
352
  end
452
353
  end
354
+
355
+ # Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Abachrome
4
- VERSION = "0.1.6"
4
+ VERSION = "0.3.0"
5
5
  end
6
6
 
7
7
  # Copyright (c) 2025 Durable Programming, LLC. All rights reserved.
data/lib/abachrome.rb CHANGED
@@ -46,8 +46,16 @@ module Abachrome
46
46
 
47
47
  module Converters
48
48
  autoload :Base, "abachrome/converters/base"
49
+ autoload :HslToSrgb, "abachrome/converters/hsl_to_srgb"
50
+ autoload :HwbToSrgb, "abachrome/converters/hwb_to_srgb"
51
+ autoload :LabToSrgb, "abachrome/converters/lab_to_srgb"
52
+ autoload :LabToXyz, "abachrome/converters/lab_to_xyz"
53
+ autoload :LchToLab, "abachrome/converters/lch_to_lab"
54
+ autoload :LchToSrgb, "abachrome/converters/lch_to_srgb"
49
55
  autoload :LrgbToOklab, "abachrome/converters/lrgb_to_oklab"
50
56
  autoload :OklabToLrgb, "abachrome/converters/oklab_to_lrgb"
57
+ autoload :SrgbToXyz, "abachrome/converters/srgb_to_xyz"
58
+ autoload :XyzToLrgb, "abachrome/converters/xyz_to_lrgb"
51
59
  end
52
60
 
53
61
  module Gamut
@@ -73,6 +81,7 @@ module Abachrome
73
81
  end
74
82
 
75
83
  module Parsers
84
+ autoload :CSS, "abachrome/parsers/css"
76
85
  autoload :Hex, "abachrome/parsers/hex"
77
86
  end
78
87
 
@@ -120,6 +129,50 @@ module Abachrome
120
129
  Color.from_oklch(l, a, b, alpha)
121
130
  end
122
131
 
132
+ # Creates a color in the HSL color space.
133
+ #
134
+ # @param h [Numeric] The hue angle in degrees (0-360)
135
+ # @param s [Numeric] The saturation component, from 0.0 to 1.0
136
+ # @param l [Numeric] The lightness component, from 0.0 to 1.0
137
+ # @param alpha [Float] The alpha (opacity) value, from 0.0 to 1.0, defaults to 1.0
138
+ # @return [Abachrome::Color] A new Color object in the HSL color space
139
+ def from_hsl(h, s, l, alpha = 1.0)
140
+ Color.from_hsl(h, s, l, alpha)
141
+ end
142
+
143
+ # Creates a color in the HWB color space.
144
+ #
145
+ # @param h [Numeric] The hue angle in degrees (0-360)
146
+ # @param w [Numeric] The whiteness component, from 0.0 to 1.0
147
+ # @param b [Numeric] The blackness component, from 0.0 to 1.0
148
+ # @param alpha [Float] The alpha (opacity) value, from 0.0 to 1.0, defaults to 1.0
149
+ # @return [Abachrome::Color] A new Color object in the HWB color space
150
+ def from_hwb(h, w, b, alpha = 1.0)
151
+ Color.from_hwb(h, w, b, alpha)
152
+ end
153
+
154
+ # Creates a color in the CIELAB color space.
155
+ #
156
+ # @param l [Numeric] The lightness component (L*), typically in range 0-100
157
+ # @param a [Numeric] The green-red component (a*) of the CIELAB color space
158
+ # @param b [Numeric] The blue-yellow component (b*) of the CIELAB color space
159
+ # @param alpha [Float] The alpha (opacity) value, from 0.0 to 1.0, defaults to 1.0
160
+ # @return [Abachrome::Color] A new Color object in the CIELAB color space
161
+ def from_lab(l, a, b, alpha = 1.0)
162
+ Color.from_lab(l, a, b, alpha)
163
+ end
164
+
165
+ # Creates a color in the CIELCH color space.
166
+ #
167
+ # @param l [Numeric] The lightness component (L*), typically in range 0-100
168
+ # @param c [Numeric] The chroma component (C*)
169
+ # @param h [Numeric] The hue angle in degrees (0-360)
170
+ # @param alpha [Float] The alpha (opacity) value, from 0.0 to 1.0, defaults to 1.0
171
+ # @return [Abachrome::Color] A new Color object in the CIELCH color space
172
+ def from_lch(l, c, h, alpha = 1.0)
173
+ Color.from_lch(l, c, h, alpha)
174
+ end
175
+
123
176
  # Creates a color object from a hexadecimal color code string.
124
177
  #
125
178
  # @param hex_str [String] The hexadecimal color code string to parse. Can be in formats like
@@ -135,15 +188,13 @@ module Abachrome
135
188
 
136
189
  # Creates a color object from a CSS color name.
137
190
  #
138
- # @param color_name [String] The CSS color name (e.g., 'red', 'blue', 'cornflowerblue').
139
- # Case-insensitive.
191
+ # @param color_name [String] The CSS color name (e.g., 'red', 'blue', 'cornflowerblue',
192
+ # 'transparent'). Case-insensitive.
140
193
  # @return [Abachrome::Color, nil] A color object in the RGB color space if the name is valid,
141
194
  # nil if the color name is not recognized.
142
195
  def from_name(color_name)
143
- rgb_values = Named::CSS::ColorNames[color_name.downcase]
144
- return nil unless rgb_values
145
-
146
- from_rgb(*rgb_values.map { |v| v / 255.0 })
196
+ require_relative "abachrome/parsers/css"
197
+ Parsers::CSS.parse_named_color(color_name.strip.downcase)
147
198
  end
148
199
 
149
200
  # Creates a color in the YIQ color space.
@@ -202,7 +253,6 @@ module Abachrome
202
253
  # @param css_string [String] The CSS color string to parse (e.g., "#ff0000", "rgb(255, 0, 0)", "red")
203
254
  # @return [Abachrome::Color, nil] A Color object if parsing succeeds, nil otherwise
204
255
  def parse(css_string)
205
- require_relative "abachrome/parsers/css"
206
256
  Parsers::CSS.parse(css_string)
207
257
  end
208
258
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: abachrome
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Durable Programming
@@ -66,14 +66,18 @@ files:
66
66
  - lib/abachrome/abc_decimal.rb
67
67
  - lib/abachrome/color.rb
68
68
  - lib/abachrome/color_mixins/blend.rb
69
+ - lib/abachrome/color_mixins/harmonies.rb
69
70
  - lib/abachrome/color_mixins/lighten.rb
70
71
  - lib/abachrome/color_mixins/spectral_mix.rb
71
72
  - lib/abachrome/color_mixins/to_colorspace.rb
72
73
  - lib/abachrome/color_mixins/to_grayscale.rb
74
+ - lib/abachrome/color_mixins/to_lms.rb
73
75
  - lib/abachrome/color_mixins/to_lrgb.rb
74
76
  - lib/abachrome/color_mixins/to_oklab.rb
75
77
  - lib/abachrome/color_mixins/to_oklch.rb
76
78
  - lib/abachrome/color_mixins/to_srgb.rb
79
+ - lib/abachrome/color_mixins/to_xyz.rb
80
+ - lib/abachrome/color_mixins/wcag.rb
77
81
  - lib/abachrome/color_models/cmyk.rb
78
82
  - lib/abachrome/color_models/hsv.rb
79
83
  - lib/abachrome/color_models/lms.rb
@@ -86,7 +90,14 @@ files:
86
90
  - lib/abachrome/converter.rb
87
91
  - lib/abachrome/converters/base.rb
88
92
  - lib/abachrome/converters/cmyk_to_srgb.rb
93
+ - lib/abachrome/converters/hsl_to_srgb.rb
94
+ - lib/abachrome/converters/hwb_to_srgb.rb
95
+ - lib/abachrome/converters/lab_to_srgb.rb
96
+ - lib/abachrome/converters/lab_to_xyz.rb
97
+ - lib/abachrome/converters/lch_to_lab.rb
98
+ - lib/abachrome/converters/lch_to_srgb.rb
89
99
  - lib/abachrome/converters/lms_to_lrgb.rb
100
+ - lib/abachrome/converters/lms_to_oklab.rb
90
101
  - lib/abachrome/converters/lms_to_srgb.rb
91
102
  - lib/abachrome/converters/lms_to_xyz.rb
92
103
  - lib/abachrome/converters/lrgb_to_lms.rb
@@ -97,18 +108,25 @@ files:
97
108
  - lib/abachrome/converters/oklab_to_lrgb.rb
98
109
  - lib/abachrome/converters/oklab_to_oklch.rb
99
110
  - lib/abachrome/converters/oklab_to_srgb.rb
111
+ - lib/abachrome/converters/oklab_to_xyz.rb
112
+ - lib/abachrome/converters/oklch_to_lms.rb
100
113
  - lib/abachrome/converters/oklch_to_lrgb.rb
101
114
  - lib/abachrome/converters/oklch_to_oklab.rb
102
115
  - lib/abachrome/converters/oklch_to_srgb.rb
103
116
  - lib/abachrome/converters/oklch_to_xyz.rb
104
117
  - lib/abachrome/converters/srgb_to_cmyk.rb
118
+ - lib/abachrome/converters/srgb_to_lms.rb
105
119
  - lib/abachrome/converters/srgb_to_lrgb.rb
106
120
  - lib/abachrome/converters/srgb_to_oklab.rb
107
121
  - lib/abachrome/converters/srgb_to_oklch.rb
122
+ - lib/abachrome/converters/srgb_to_xyz.rb
108
123
  - lib/abachrome/converters/srgb_to_yiq.rb
109
124
  - lib/abachrome/converters/xyz_to_lms.rb
125
+ - lib/abachrome/converters/xyz_to_lrgb.rb
110
126
  - lib/abachrome/converters/xyz_to_oklab.rb
127
+ - lib/abachrome/converters/xyz_to_srgb.rb
111
128
  - lib/abachrome/converters/yiq_to_srgb.rb
129
+ - lib/abachrome/floatify.rb
112
130
  - lib/abachrome/gamut/base.rb
113
131
  - lib/abachrome/gamut/p3.rb
114
132
  - lib/abachrome/gamut/rec2020.rb