Class: Abachrome::Parsers::CSS

Inherits:
Object
  • Object
show all
Defined in:
lib/abachrome/parsers/css.rb

Class Method Summary collapse

Class Method Details

.hsl_to_rgb(h, s, l) ⇒ Object

Color space conversion functions



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/abachrome/parsers/css.rb', line 353

def self.hsl_to_rgb(h, s, l)
  h /= 360.0 # Normalize hue to 0-1

  c = (1 - ((2 * l) - 1).abs) * s
  x = c * (1 - (((h * 6) % 2) - 1).abs)
  m = l - (c / 2)

  if h < 1.0 / 6
    r = c
    g = x
    b = 0
  elsif h < 2.0 / 6
    r = x
    g = c
    b = 0
  elsif h < 3.0 / 6
    r = 0
    g = c
    b = x
  elsif h < 4.0 / 6
    r = 0
    g = x
    b = c
  elsif h < 5.0 / 6
    r = x
    g = 0
    b = c
  else
    r = c
    g = 0
    b = x
  end

  [r + m, g + m, b + m]
end

.hwb_to_rgb(h, w, b) ⇒ Object



389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/abachrome/parsers/css.rb', line 389

def self.hwb_to_rgb(h, w, b)
  # Normalize values
  h /= 360.0

  # Calculate RGB from HSL equivalent
  if w + b >= 1
    gray = w / (w + b)
    [gray, gray, gray]
  else
    rgb = hsl_to_rgb(h * 360, 1, 0.5)
    r, g, b_rgb = rgb

    # Apply whiteness and blackness
    r = (r * (1 - w - b)) + w
    g = (g * (1 - w - b)) + w
    b_rgb = (b_rgb * (1 - w - b)) + w

    [r, g, b_rgb]
  end
end

.lab_to_xyz(l, a, b) ⇒ Object



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/abachrome/parsers/css.rb', line 410

def self.lab_to_xyz(l, a, b)
  # CIELAB to XYZ conversion (D65 white point)
  y = (l + 16) / 116
  x = (a / 500) + y
  z = y - (b / 200)

  x = x**3 > 0.008856 ? x**3 : (x - (16 / 116)) / 7.787
  y = y**3 > 0.008856 ? y**3 : (y - (16 / 116)) / 7.787
  z = z**3 > 0.008856 ? z**3 : (z - (16 / 116)) / 7.787

  # D65 white point
  x *= 0.95047
  y *= 1.0
  z *= 1.08883

  [x, y, z]
end

.lch_to_lab(l, c, h) ⇒ Object



428
429
430
431
432
433
# File 'lib/abachrome/parsers/css.rb', line 428

def self.lch_to_lab(l, c, h)
  h_rad = h * Math::PI / 180.0
  a = c * Math.cos(h_rad)
  b = c * Math.sin(h_rad)
  [l, a, b]
end

.parse(input) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/abachrome/parsers/css.rb', line 24

def self.parse(input)
  return nil unless input.is_a?(String)

  input = input.strip.downcase

  # Try named colors first
  named_color = parse_named_color(input)
  return named_color if named_color

  # Try hex colors
  hex_color = Hex.parse(input)
  return hex_color if hex_color

  # Try functional notation
  parse_functional_color(input)
end

.parse_angle_value(str) ⇒ Object



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/abachrome/parsers/css.rb', line 333

def self.parse_angle_value(str)
  return nil unless str

  if str.end_with?("deg")
    str.chomp("deg").to_f
  elsif str.end_with?("rad")
    str.chomp("rad").to_f * 180.0 / Math::PI
  elsif str.end_with?("grad")
    str.chomp("grad").to_f * 0.9
  elsif str.end_with?("turn")
    str.chomp("turn").to_f * 360.0
  else
    str.to_f # Assume degrees
  end
rescue StandardError
  nil
end

.parse_color_function(params) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/abachrome/parsers/css.rb', line 160

def self.parse_color_function(params)
  # Parse color(space values...)
  parts = params.split(/\s+/, 2)
  return nil unless parts.length == 2

  space = parts[0]
  values_str = parts[1]

  case space
  when "srgb"
    values = parse_color_values(values_str, 3)
    return nil unless values

    r, g, b = values
    Color.from_rgb(r, g, b)
  when "srgb-linear"
    values = parse_color_values(values_str, 3)
    return nil unless values

    r, g, b = values
    Color.from_lrgb(r, g, b)
  when "display-p3"
    # For now, approximate as sRGB
    values = parse_color_values(values_str, 3)
    return nil unless values

    r, g, b = values
    Color.from_rgb(r, g, b)
  when "a98-rgb"
    # For now, approximate as sRGB
    values = parse_color_values(values_str, 3)
    return nil unless values

    r, g, b = values
    Color.from_rgb(r, g, b)
  when "prophoto-rgb"
    # For now, approximate as sRGB
    values = parse_color_values(values_str, 3)
    return nil unless values

    r, g, b = values
    Color.from_rgb(r, g, b)
  when "rec2020"
    # For now, approximate as sRGB
    values = parse_color_values(values_str, 3)
    return nil unless values

    r, g, b = values
    Color.from_rgb(r, g, b)
  end
end

.parse_color_values(str, expected_count) ⇒ Object

Helper methods for parsing values



214
215
216
217
218
219
220
221
# File 'lib/abachrome/parsers/css.rb', line 214

def self.parse_color_values(str, expected_count)
  values = str.split(/\s*,\s*/).map(&:strip)
  return nil unless values.length == expected_count

  values.map do |v|
    parse_numeric_value(v)
  end.compact
end

.parse_functional_color(input) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/abachrome/parsers/css.rb', line 53

def self.parse_functional_color(input)
  case input
  when /^rgb\((.+)\)$/
    parse_rgb(::Regexp.last_match(1))
  when /^rgba\((.+)\)$/
    parse_rgba(::Regexp.last_match(1))
  when /^hsl\((.+)\)$/
    parse_hsl(::Regexp.last_match(1))
  when /^hsla\((.+)\)$/
    parse_hsla(::Regexp.last_match(1))
  when /^hwb\((.+)\)$/
    parse_hwb(::Regexp.last_match(1))
  when /^lab\((.+)\)$/
    parse_lab(::Regexp.last_match(1))
  when /^lch\((.+)\)$/
    parse_lch(::Regexp.last_match(1))
  when /^oklab\((.+)\)$/
    parse_oklab(::Regexp.last_match(1))
  when /^oklch\((.+)\)$/
    parse_oklch(::Regexp.last_match(1))
  when /^color\((.+)\)$/
    parse_color_function(::Regexp.last_match(1))
  end
end

.parse_hsl(params) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/abachrome/parsers/css.rb', line 94

def self.parse_hsl(params)
  values = parse_hsl_values(params, 3)
  return nil unless values

  h, s, l = values
  rgb = hsl_to_rgb(h, s, l)
  Color.from_rgb(*rgb)
end

.parse_hsl_values(str, expected_count) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/abachrome/parsers/css.rb', line 223

def self.parse_hsl_values(str, expected_count)
  values = str.split(/\s*,\s*/).map(&:strip)
  return nil unless values.length == expected_count

  parsed = []
  values.each_with_index do |v, i|
    val = if i.zero? # Hue
            parse_angle_value(v)

          else # Saturation, Lightness, Alpha
            parse_percentage_or_number(v)

          end
    return nil unless val

    parsed << val
  end
  parsed
end

.parse_hsla(params) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/abachrome/parsers/css.rb', line 103

def self.parse_hsla(params)
  values = parse_hsl_values(params, 4)
  return nil unless values

  h, s, l, a = values
  rgb = hsl_to_rgb(h, s, l)
  Color.from_rgb(*rgb, a)
end

.parse_hwb(params) ⇒ Object



112
113
114
115
116
117
118
119
# File 'lib/abachrome/parsers/css.rb', line 112

def self.parse_hwb(params)
  values = parse_hwb_values(params)
  return nil unless values

  h, w, b, a = values
  rgb = hwb_to_rgb(h, w, b)
  Color.from_rgb(*rgb, a)
end

.parse_hwb_values(str) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/abachrome/parsers/css.rb', line 243

def self.parse_hwb_values(str)
  values = str.split(/\s*,\s*/).map(&:strip)
  return nil unless values.length >= 3

  h = parse_angle_value(values[0])
  w = parse_percentage_or_number(values[1])
  b = parse_percentage_or_number(values[2])
  a = values[3] ? parse_numeric_value(values[3]) : 1.0

  return nil unless h && w && b && a

  [h, w, b, a]
end

.parse_lab(params) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'lib/abachrome/parsers/css.rb', line 121

def self.parse_lab(params)
  values = parse_lab_values(params, 3)
  return nil unless values

  l, a, b = values
  # Convert CIELAB to XYZ, then to sRGB
  xyz = lab_to_xyz(l, a, b)
  rgb = xyz_to_rgb(*xyz)
  Color.from_rgb(*rgb)
end

.parse_lab_values(str, expected_count) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/abachrome/parsers/css.rb', line 257

def self.parse_lab_values(str, expected_count)
  values = str.split(/\s+/, expected_count).map(&:strip)
  return nil unless values.length == expected_count

  l = parse_percentage_or_number(values[0])
  a = parse_numeric_value(values[1])
  b = parse_numeric_value(values[2])

  return nil unless l && a && b

  [l, a, b]
end

.parse_lch(params) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/abachrome/parsers/css.rb', line 132

def self.parse_lch(params)
  values = parse_lch_values(params, 3)
  return nil unless values

  l, c, h = values
  # Convert CIELCH to CIELAB, then to XYZ, then to sRGB
  lab = lch_to_lab(l, c, h)
  xyz = lab_to_xyz(*lab)
  rgb = xyz_to_rgb(*xyz)
  Color.from_rgb(*rgb)
end

.parse_lch_values(str, expected_count) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/abachrome/parsers/css.rb', line 270

def self.parse_lch_values(str, expected_count)
  values = str.split(/\s+/, expected_count).map(&:strip)
  return nil unless values.length == expected_count

  l = parse_percentage_or_number(values[0])
  c = parse_numeric_value(values[1])
  h = parse_angle_value(values[2])

  return nil unless l && c && h

  [l, c, h]
end

.parse_named_color(input) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/abachrome/parsers/css.rb', line 41

def self.parse_named_color(input)
  # Check if input matches a named color
  rgb_values = Named::CSS.method(input.to_sym)&.call
  return nil unless rgb_values

  # Convert 0-255 RGB values to 0-1 range
  r, g, b = rgb_values.map { |v| v / 255.0 }
  Color.from_rgb(r, g, b)
rescue NameError
  nil
end

.parse_numeric_value(str) ⇒ Object



309
310
311
312
313
314
315
316
317
318
319
# File 'lib/abachrome/parsers/css.rb', line 309

def self.parse_numeric_value(str)
  return nil unless str

  if str.end_with?("%")
    (str.chomp("%").to_f / 100.0)
  else
    str.to_f
  end
rescue StandardError
  nil
end

.parse_oklab(params) ⇒ Object



144
145
146
147
148
149
150
# File 'lib/abachrome/parsers/css.rb', line 144

def self.parse_oklab(params)
  values = parse_oklab_values(params, 3)
  return nil unless values

  l, a, b = values
  Color.from_oklab(l, a, b)
end

.parse_oklab_values(str, expected_count) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/abachrome/parsers/css.rb', line 283

def self.parse_oklab_values(str, expected_count)
  values = str.split(/\s+/, expected_count).map(&:strip)
  return nil unless values.length == expected_count

  l = parse_percentage_or_number(values[0])
  a = parse_numeric_value(values[1])
  b = parse_numeric_value(values[2])

  return nil unless l && a && b

  [l, a, b]
end

.parse_oklch(params) ⇒ Object



152
153
154
155
156
157
158
# File 'lib/abachrome/parsers/css.rb', line 152

def self.parse_oklch(params)
  values = parse_oklch_values(params, 3)
  return nil unless values

  l, c, h = values
  Color.from_oklch(l, c, h)
end

.parse_oklch_values(str, expected_count) ⇒ Object



296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/abachrome/parsers/css.rb', line 296

def self.parse_oklch_values(str, expected_count)
  values = str.split(/\s+/, expected_count).map(&:strip)
  return nil unless values.length == expected_count

  l = parse_percentage_or_number(values[0])
  c = parse_numeric_value(values[1])
  h = parse_angle_value(values[2])

  return nil unless l && c && h

  [l, c, h]
end

.parse_percentage_or_number(str) ⇒ Object



321
322
323
324
325
326
327
328
329
330
331
# File 'lib/abachrome/parsers/css.rb', line 321

def self.parse_percentage_or_number(str)
  return nil unless str

  if str.end_with?("%")
    str.chomp("%").to_f / 100.0
  else
    str.to_f
  end
rescue StandardError
  nil
end

.parse_rgb(params) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/abachrome/parsers/css.rb', line 78

def self.parse_rgb(params)
  values = parse_color_values(params, 3)
  return nil unless values

  r, g, b = values
  Color.from_rgb(r, g, b)
end

.parse_rgba(params) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/abachrome/parsers/css.rb', line 86

def self.parse_rgba(params)
  values = parse_color_values(params, 4)
  return nil unless values

  r, g, b, a = values
  Color.from_rgb(r, g, b, a)
end

.xyz_to_rgb(x, y, z) ⇒ Object



435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/abachrome/parsers/css.rb', line 435

def self.xyz_to_rgb(x, y, z)
  # XYZ to linear RGB
  r = (x * 3.2406) + (y * -1.5372) + (z * -0.4986)
  g = (x * -0.9689) + (y * 1.8758) + (z *  0.0415)
  b = (x * 0.0557) + (y * -0.2040) + (z *  1.0570)

  # Linear RGB to sRGB
  [r, g, b].map do |v|
    if v > 0.0031308
      (1.055 * (v**(1 / 2.4))) - 0.055
    else
      12.92 * v
    end
  end
end