Class: Philiprehberger::ColorConvert::Color
- Inherits:
-
Object
- Object
- Philiprehberger::ColorConvert::Color
- Defined in:
- lib/philiprehberger/color_convert/color.rb
Overview
Represents a color with conversion, manipulation, and comparison methods.
Constant Summary collapse
- COLOR_BLINDNESS_MATRICES =
Color blindness simulation matrices (Brettel/Vienot method).
{ protanopia: [ [0.152286, 1.052583, -0.204868], [0.114503, 0.786281, 0.099216], [-0.003882, -0.048116, 1.051998] ], deuteranopia: [ [0.367322, 0.860646, -0.227968], [0.280085, 0.672501, 0.047413], [-0.011820, 0.042940, 0.968881] ], tritanopia: [ [1.255528, -0.076749, -0.178779], [-0.078411, 0.930809, 0.147602], [0.004733, 0.691367, 0.303900] ] }.freeze
Instance Attribute Summary collapse
-
#alpha ⇒ Float
readonly
Alpha component (0.0-1.0).
-
#b ⇒ Integer
readonly
Blue component (0-255).
-
#g ⇒ Integer
readonly
Green component (0-255).
-
#r ⇒ Integer
readonly
Red component (0-255).
Class Method Summary collapse
- .delinearize_srgb_class(c) ⇒ Object private
-
.from_cmyk(c, m, y, k) ⇒ Color
Create a Color from CMYK values.
-
.from_hsl(h, s, l, alpha: 1.0) ⇒ Color
Create a Color from HSL values.
-
.from_lab(l, a, b) ⇒ Color
Create a Color from CIELAB values (D65 illuminant).
-
.from_xyz(x, y, z) ⇒ Color
Create a Color from CIE XYZ values.
- .hue_to_rgb(p, q, t) ⇒ Object private
- .lab_f_inv(t) ⇒ Object private
Instance Method Summary collapse
- #==(other) ⇒ Boolean
-
#analogous ⇒ Array<Color>
Generate analogous colors (30 degrees apart on the color wheel).
-
#blend(other, weight: 0.5) ⇒ Color
Blend this color with another color.
-
#complement ⇒ Color
Return the complementary color (180 degrees on the color wheel).
-
#contrast_ratio(other) ⇒ Float
Calculate the WCAG contrast ratio between this color and another.
-
#cool? ⇒ Boolean
True if the color temperature is cool.
-
#darken(amount) ⇒ Color
Darken the color by a percentage.
-
#desaturate(amount) ⇒ Color
Decrease saturation by a percentage.
-
#gradient(other, steps: 5) ⇒ Array<Color>
Generate a gradient palette between this color and another.
-
#grayscale ⇒ Color
Return a grayscale version using the standard ITU-R BT.601 luma formula (0.299*R + 0.587*G + 0.114*B).
-
#initialize(r, g, b, alpha: 1.0) ⇒ Color
constructor
A new instance of Color.
-
#invert ⇒ Color
Return the inverted color (negative) by flipping each RGB channel.
-
#lighten(amount) ⇒ Color
Lighten the color by a percentage.
-
#monochromatic(steps: 5) ⇒ Array<Color>
Generate a monochromatic palette by varying lightness.
-
#opacity ⇒ Float
Return the opacity (alpha) value.
-
#opaque? ⇒ Boolean
True if the color is fully opaque (alpha == 1.0).
-
#relative_luminance ⇒ Float
Calculate relative luminance per WCAG 2.0.
-
#saturate(amount) ⇒ Color
Increase saturation by a percentage.
-
#simulate_color_blindness(type) ⇒ Color
Simulate color blindness.
-
#split_complementary ⇒ Array<Color>
Generate split-complementary colors (150 and 210 degrees from base).
-
#temperature ⇒ Symbol
Classify the color temperature based on HSL hue.
-
#tetradic ⇒ Array<Color>
Generate tetradic (rectangular) colors (90 degrees apart).
-
#to_cmyk ⇒ Hash
Convert to CMYK hash.
-
#to_hex ⇒ String
Convert to hex string.
-
#to_hsl ⇒ Hash
Convert to HSL hash.
-
#to_hsv ⇒ Hash
Convert to HSV hash.
-
#to_lab ⇒ Hash
Convert to CIELAB hash via XYZ (D65 illuminant).
-
#to_rgb ⇒ Hash
Convert to RGB hash.
-
#to_rgba ⇒ Hash
Convert to RGBA hash.
- #to_s ⇒ String
-
#to_short_hex ⇒ String
Convert to short hex string when possible.
-
#to_xyz ⇒ Hash
Convert to CIE XYZ color space (D65 illuminant).
-
#transparent? ⇒ Boolean
True if the color has any transparency (alpha < 1.0).
-
#triadic ⇒ Array<Color>
Generate triadic colors (120 degrees apart on the color wheel).
-
#warm? ⇒ Boolean
True if the color temperature is warm.
-
#wcag_aa?(bg:, large: false) ⇒ Boolean
Check whether contrast against a background meets WCAG AA.
-
#wcag_aaa?(bg:, large: false) ⇒ Boolean
Check whether contrast against a background meets WCAG AAA.
-
#with_opacity(val) ⇒ Color
Return a new Color with the given opacity.
Constructor Details
#initialize(r, g, b, alpha: 1.0) ⇒ Color
Returns a new instance of Color.
23 24 25 26 27 28 |
# File 'lib/philiprehberger/color_convert/color.rb', line 23 def initialize(r, g, b, alpha: 1.0) @r = clamp(r.round, 0, 255) @g = clamp(g.round, 0, 255) @b = clamp(b.round, 0, 255) @alpha = clamp(alpha.to_f, 0.0, 1.0) end |
Instance Attribute Details
#alpha ⇒ Float (readonly)
Returns alpha component (0.0-1.0).
17 18 19 |
# File 'lib/philiprehberger/color_convert/color.rb', line 17 def alpha @alpha end |
#b ⇒ Integer (readonly)
Returns blue component (0-255).
14 15 16 |
# File 'lib/philiprehberger/color_convert/color.rb', line 14 def b @b end |
#g ⇒ Integer (readonly)
Returns green component (0-255).
11 12 13 |
# File 'lib/philiprehberger/color_convert/color.rb', line 11 def g @g end |
#r ⇒ Integer (readonly)
Returns red component (0-255).
8 9 10 |
# File 'lib/philiprehberger/color_convert/color.rb', line 8 def r @r end |
Class Method Details
.delinearize_srgb_class(c) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
580 581 582 583 584 585 586 |
# File 'lib/philiprehberger/color_convert/color.rb', line 580 def self.delinearize_srgb_class(c) if c <= 0.0031308 12.92 * c else (1.055 * (c**(1.0 / 2.4))) - 0.055 end end |
.from_cmyk(c, m, y, k) ⇒ Color
Create a Color from CMYK values.
501 502 503 504 505 506 507 508 509 510 511 512 |
# File 'lib/philiprehberger/color_convert/color.rb', line 501 def self.from_cmyk(c, m, y, k) c /= 100.0 m /= 100.0 y /= 100.0 k /= 100.0 r = 255 * (1 - c) * (1 - k) g = 255 * (1 - m) * (1 - k) b = 255 * (1 - y) * (1 - k) new(r.round, g.round, b.round) end |
.from_hsl(h, s, l, alpha: 1.0) ⇒ Color
Create a Color from HSL values.
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 |
# File 'lib/philiprehberger/color_convert/color.rb', line 474 def self.from_hsl(h, s, l, alpha: 1.0) h /= 360.0 s /= 100.0 l /= 100.0 if s.zero? val = (l * 255).round return new(val, val, val, alpha: alpha) end q = l < 0.5 ? l * (1 + s) : l + s - (l * s) p = (2 * l) - q r = hue_to_rgb(p, q, h + (1.0 / 3)) g = hue_to_rgb(p, q, h) b = hue_to_rgb(p, q, h - (1.0 / 3)) new((r * 255).round, (g * 255).round, (b * 255).round, alpha: alpha) end |
.from_lab(l, a, b) ⇒ Color
Create a Color from CIELAB values (D65 illuminant).
520 521 522 523 524 525 526 527 528 529 530 531 |
# File 'lib/philiprehberger/color_convert/color.rb', line 520 def self.from_lab(l, a, b) # LAB to XYZ fy = (l + 16.0) / 116.0 fx = (a / 500.0) + fy fz = fy - (b / 200.0) x = lab_f_inv(fx) * 95.047 y = lab_f_inv(fy) * 100.0 z = lab_f_inv(fz) * 108.883 from_xyz(x, y, z) end |
.from_xyz(x, y, z) ⇒ Color
Create a Color from CIE XYZ values.
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 |
# File 'lib/philiprehberger/color_convert/color.rb', line 539 def self.from_xyz(x, y, z) x /= 100.0 y /= 100.0 z /= 100.0 r = (x * 3.2404542) + (y * -1.5371385) + (z * -0.4985314) g = (x * -0.9692660) + (y * 1.8760108) + (z * 0.0415560) b = (x * 0.0556434) + (y * -0.2040259) + (z * 1.0572252) r = delinearize_srgb_class(r) g = delinearize_srgb_class(g) b = delinearize_srgb_class(b) new( [[r * 255, 0].max, 255].min.round, [[g * 255, 0].max, 255].min.round, [[b * 255, 0].max, 255].min.round ) end |
.hue_to_rgb(p, q, t) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
560 561 562 563 564 565 566 567 568 |
# File 'lib/philiprehberger/color_convert/color.rb', line 560 def self.hue_to_rgb(p, q, t) t += 1 if t.negative? t -= 1 if t > 1 return p + ((q - p) * 6 * t) if t < 1.0 / 6 return q if t < 1.0 / 2 return p + ((q - p) * ((2.0 / 3) - t) * 6) if t < 2.0 / 3 p end |
.lab_f_inv(t) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
571 572 573 574 575 576 577 |
# File 'lib/philiprehberger/color_convert/color.rb', line 571 def self.lab_f_inv(t) if t > 6.0 / 29 t**3 else 3.0 * ((6.0 / 29)**2) * (t - (4.0 / 29)) end end |
Instance Method Details
#==(other) ⇒ Boolean
463 464 465 |
# File 'lib/philiprehberger/color_convert/color.rb', line 463 def ==(other) other.is_a?(Color) && @r == other.r && @g == other.g && @b == other.b && @alpha == other.alpha end |
#analogous ⇒ Array<Color>
Generate analogous colors (30 degrees apart on the color wheel).
284 285 286 287 288 289 290 291 |
# File 'lib/philiprehberger/color_convert/color.rb', line 284 def analogous hsl = to_hsl [ self.class.from_hsl((hsl[:h] - 30) % 360, hsl[:s], hsl[:l]), self.class.new(@r, @g, @b), self.class.from_hsl((hsl[:h] + 30) % 360, hsl[:s], hsl[:l]) ] end |
#blend(other, weight: 0.5) ⇒ Color
Blend this color with another color.
273 274 275 276 277 278 279 |
# File 'lib/philiprehberger/color_convert/color.rb', line 273 def blend(other, weight: 0.5) w = clamp(weight.to_f, 0.0, 1.0) new_r = (@r * (1.0 - w)) + (other.r * w) new_g = (@g * (1.0 - w)) + (other.g * w) new_b = (@b * (1.0 - w)) + (other.b * w) self.class.new(new_r.round, new_g.round, new_b.round) end |
#complement ⇒ Color
Return the complementary color (180 degrees on the color wheel).
244 245 246 247 248 |
# File 'lib/philiprehberger/color_convert/color.rb', line 244 def complement hsl = to_hsl new_h = (hsl[:h] + 180) % 360 self.class.from_hsl(new_h, hsl[:s], hsl[:l]) end |
#contrast_ratio(other) ⇒ Float
Calculate the WCAG contrast ratio between this color and another.
392 393 394 395 396 397 398 |
# File 'lib/philiprehberger/color_convert/color.rb', line 392 def contrast_ratio(other) l1 = relative_luminance l2 = coerce_color(other).relative_luminance lighter = [l1, l2].max darker = [l1, l2].min ((lighter + 0.05) / (darker + 0.05)).round(2) end |
#cool? ⇒ Boolean
Returns true if the color temperature is cool.
451 452 453 |
# File 'lib/philiprehberger/color_convert/color.rb', line 451 def cool? temperature == :cool end |
#darken(amount) ⇒ Color
Darken the color by a percentage.
219 220 221 |
# File 'lib/philiprehberger/color_convert/color.rb', line 219 def darken(amount) lighten(-amount) end |
#desaturate(amount) ⇒ Color
Decrease saturation by a percentage.
237 238 239 |
# File 'lib/philiprehberger/color_convert/color.rb', line 237 def desaturate(amount) saturate(-amount) end |
#gradient(other, steps: 5) ⇒ Array<Color>
Generate a gradient palette between this color and another.
366 367 368 369 370 371 372 |
# File 'lib/philiprehberger/color_convert/color.rb', line 366 def gradient(other, steps: 5) steps = [steps, 2].max (0...steps).map do |i| weight = i / (steps - 1).to_f blend(other, weight: weight) end end |
#grayscale ⇒ Color
Return a grayscale version using the standard ITU-R BT.601 luma formula (0.299*R + 0.587*G + 0.114*B). All three channels are set to the resulting luma value. Alpha is preserved.
263 264 265 266 |
# File 'lib/philiprehberger/color_convert/color.rb', line 263 def grayscale luma = ((0.299 * @r) + (0.587 * @g) + (0.114 * @b)).round self.class.new(luma, luma, luma, alpha: @alpha) end |
#invert ⇒ Color
Return the inverted color (negative) by flipping each RGB channel. Alpha is preserved.
254 255 256 |
# File 'lib/philiprehberger/color_convert/color.rb', line 254 def invert self.class.new(255 - @r, 255 - @g, 255 - @b, alpha: @alpha) end |
#lighten(amount) ⇒ Color
Lighten the color by a percentage.
209 210 211 212 213 |
# File 'lib/philiprehberger/color_convert/color.rb', line 209 def lighten(amount) hsl = to_hsl new_l = clamp(hsl[:l] + amount, 0, 100) self.class.from_hsl(hsl[:h], hsl[:s], new_l) end |
#monochromatic(steps: 5) ⇒ Array<Color>
Generate a monochromatic palette by varying lightness.
378 379 380 381 382 383 384 385 386 |
# File 'lib/philiprehberger/color_convert/color.rb', line 378 def monochromatic(steps: 5) steps = [steps, 2].max hsl = to_hsl step_size = 100.0 / (steps + 1) (1..steps).map do |i| self.class.from_hsl(hsl[:h], hsl[:s], step_size * i) end end |
#opacity ⇒ Float
Return the opacity (alpha) value.
68 69 70 |
# File 'lib/philiprehberger/color_convert/color.rb', line 68 def opacity @alpha end |
#opaque? ⇒ Boolean
Returns true if the color is fully opaque (alpha == 1.0).
81 82 83 |
# File 'lib/philiprehberger/color_convert/color.rb', line 81 def opaque? (@alpha - 1.0).abs < Float::EPSILON end |
#relative_luminance ⇒ Float
Calculate relative luminance per WCAG 2.0.
423 424 425 426 427 428 |
# File 'lib/philiprehberger/color_convert/color.rb', line 423 def relative_luminance rs = linearize(@r / 255.0) gs = linearize(@g / 255.0) bs = linearize(@b / 255.0) (0.2126 * rs) + (0.7152 * gs) + (0.0722 * bs) end |
#saturate(amount) ⇒ Color
Increase saturation by a percentage.
227 228 229 230 231 |
# File 'lib/philiprehberger/color_convert/color.rb', line 227 def saturate(amount) hsl = to_hsl new_s = clamp(hsl[:s] + amount, 0, 100) self.class.from_hsl(hsl[:h], new_s, hsl[:l]) end |
#simulate_color_blindness(type) ⇒ Color
Simulate color blindness.
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/philiprehberger/color_convert/color.rb', line 335 def simulate_color_blindness(type) matrix = COLOR_BLINDNESS_MATRICES[type] raise ArgumentError, "Unknown color blindness type: #{type}" unless matrix rf = @r / 255.0 gf = @g / 255.0 bf = @b / 255.0 # Convert to linear RGB rl = linearize_srgb(rf) gl = linearize_srgb(gf) bl = linearize_srgb(bf) # Apply simulation matrix new_r = (matrix[0][0] * rl) + (matrix[0][1] * gl) + (matrix[0][2] * bl) new_g = (matrix[1][0] * rl) + (matrix[1][1] * gl) + (matrix[1][2] * bl) new_b = (matrix[2][0] * rl) + (matrix[2][1] * gl) + (matrix[2][2] * bl) # Convert back to sRGB new_r = delinearize_srgb(clamp(new_r, 0.0, 1.0)) new_g = delinearize_srgb(clamp(new_g, 0.0, 1.0)) new_b = delinearize_srgb(clamp(new_b, 0.0, 1.0)) self.class.new((new_r * 255).round, (new_g * 255).round, (new_b * 255).round) end |
#split_complementary ⇒ Array<Color>
Generate split-complementary colors (150 and 210 degrees from base).
321 322 323 324 325 326 327 328 |
# File 'lib/philiprehberger/color_convert/color.rb', line 321 def split_complementary hsl = to_hsl [ self.class.new(@r, @g, @b), self.class.from_hsl((hsl[:h] + 150) % 360, hsl[:s], hsl[:l]), self.class.from_hsl((hsl[:h] + 210) % 360, hsl[:s], hsl[:l]) ] end |
#temperature ⇒ Symbol
Classify the color temperature based on HSL hue.
433 434 435 436 437 438 439 440 441 442 443 |
# File 'lib/philiprehberger/color_convert/color.rb', line 433 def temperature hue = to_hsl[:h] if hue <= 60 || hue >= 300 :warm elsif hue.between?(120, 240) :cool else :neutral end end |
#tetradic ⇒ Array<Color>
Generate tetradic (rectangular) colors (90 degrees apart).
308 309 310 311 312 313 314 315 316 |
# File 'lib/philiprehberger/color_convert/color.rb', line 308 def tetradic hsl = to_hsl [ self.class.new(@r, @g, @b), self.class.from_hsl((hsl[:h] + 90) % 360, hsl[:s], hsl[:l]), self.class.from_hsl((hsl[:h] + 180) % 360, hsl[:s], hsl[:l]), self.class.from_hsl((hsl[:h] + 270) % 360, hsl[:s], hsl[:l]) ] end |
#to_cmyk ⇒ Hash
Convert to CMYK hash.
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/philiprehberger/color_convert/color.rb', line 152 def to_cmyk rf = @r / 255.0 gf = @g / 255.0 bf = @b / 255.0 k = 1.0 - [rf, gf, bf].max if k >= 1.0 return { c: 0.0, m: 0.0, y: 0.0, k: 100.0 } end c = (1.0 - rf - k) / (1.0 - k) m = (1.0 - gf - k) / (1.0 - k) y = (1.0 - bf - k) / (1.0 - k) { c: (c * 100).round(1), m: (m * 100).round(1), y: (y * 100).round(1), k: (k * 100).round(1) } end |
#to_hex ⇒ String
Convert to hex string.
33 34 35 |
# File 'lib/philiprehberger/color_convert/color.rb', line 33 def to_hex format('#%<r>02x%<g>02x%<b>02x', r: @r, g: @g, b: @b) end |
#to_hsl ⇒ Hash
Convert to HSL hash.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/philiprehberger/color_convert/color.rb', line 93 def to_hsl rf = @r / 255.0 gf = @g / 255.0 bf = @b / 255.0 max = [rf, gf, bf].max min = [rf, gf, bf].min l = (max + min) / 2.0 if max == min h = 0.0 s = 0.0 else d = max - min s = l > 0.5 ? d / (2.0 - max - min) : d / (max + min) h = case max when rf then ((gf - bf) / d) + (gf < bf ? 6 : 0) when gf then ((bf - rf) / d) + 2 else ((rf - gf) / d) + 4 end h /= 6.0 end { h: (h * 360).round(1), s: (s * 100).round(1), l: (l * 100).round(1) } end |
#to_hsv ⇒ Hash
Convert to HSV hash.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/philiprehberger/color_convert/color.rb', line 122 def to_hsv rf = @r / 255.0 gf = @g / 255.0 bf = @b / 255.0 max = [rf, gf, bf].max min = [rf, gf, bf].min d = max - min v = max s = max.zero? ? 0.0 : d / max if max == min h = 0.0 else h = case max when rf then ((gf - bf) / d) + (gf < bf ? 6 : 0) when gf then ((bf - rf) / d) + 2 else ((rf - gf) / d) + 4 end h /= 6.0 end { h: (h * 360).round(1), s: (s * 100).round(1), v: (v * 100).round(1) } end |
#to_lab ⇒ Hash
Convert to CIELAB hash via XYZ (D65 illuminant).
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/philiprehberger/color_convert/color.rb', line 173 def to_lab xyz = to_xyz x = xyz[:x] / 95.047 y = xyz[:y] / 100.0 z = xyz[:z] / 108.883 x = lab_f(x) y = lab_f(y) z = lab_f(z) l = (116.0 * y) - 16.0 a = 500.0 * (x - y) b = 200.0 * (y - z) { l: l.round(2), a: a.round(2), b: b.round(2) } end |
#to_rgb ⇒ Hash
Convert to RGB hash.
54 55 56 |
# File 'lib/philiprehberger/color_convert/color.rb', line 54 def to_rgb { r: @r, g: @g, b: @b } end |
#to_rgba ⇒ Hash
Convert to RGBA hash.
61 62 63 |
# File 'lib/philiprehberger/color_convert/color.rb', line 61 def to_rgba { r: @r, g: @g, b: @b, a: @alpha } end |
#to_s ⇒ String
456 457 458 459 460 |
# File 'lib/philiprehberger/color_convert/color.rb', line 456 def to_s return to_hex if opaque? format('rgba(%d, %d, %d, %s)', @r, @g, @b, @alpha) end |
#to_short_hex ⇒ String
Convert to short hex string when possible.
Returns a 3-digit hex when each channel byte collapses to a doubled hex digit (e.g. 0xff, 0xaa); otherwise returns the full 6-digit hex.
43 44 45 46 47 48 49 |
# File 'lib/philiprehberger/color_convert/color.rb', line 43 def to_short_hex if [@r, @g, @b].all? { |c| (c >> 4) == (c & 0xF) } format('#%01x%01x%01x', @r >> 4, @g >> 4, @b >> 4) else to_hex end end |
#to_xyz ⇒ Hash
Convert to CIE XYZ color space (D65 illuminant).
193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/philiprehberger/color_convert/color.rb', line 193 def to_xyz rf = linearize_srgb(@r / 255.0) * 100.0 gf = linearize_srgb(@g / 255.0) * 100.0 bf = linearize_srgb(@b / 255.0) * 100.0 x = (rf * 0.4124564) + (gf * 0.3575761) + (bf * 0.1804375) y = (rf * 0.2126729) + (gf * 0.7151522) + (bf * 0.0721750) z = (rf * 0.0193339) + (gf * 0.1191920) + (bf * 0.9503041) { x: x.round(4), y: y.round(4), z: z.round(4) } end |
#transparent? ⇒ Boolean
Returns true if the color has any transparency (alpha < 1.0).
86 87 88 |
# File 'lib/philiprehberger/color_convert/color.rb', line 86 def transparent? !opaque? end |
#triadic ⇒ Array<Color>
Generate triadic colors (120 degrees apart on the color wheel).
296 297 298 299 300 301 302 303 |
# File 'lib/philiprehberger/color_convert/color.rb', line 296 def triadic hsl = to_hsl [ self.class.new(@r, @g, @b), self.class.from_hsl((hsl[:h] + 120) % 360, hsl[:s], hsl[:l]), self.class.from_hsl((hsl[:h] + 240) % 360, hsl[:s], hsl[:l]) ] end |
#warm? ⇒ Boolean
Returns true if the color temperature is warm.
446 447 448 |
# File 'lib/philiprehberger/color_convert/color.rb', line 446 def warm? temperature == :warm end |
#wcag_aa?(bg:, large: false) ⇒ Boolean
Check whether contrast against a background meets WCAG AA.
405 406 407 408 |
# File 'lib/philiprehberger/color_convert/color.rb', line 405 def wcag_aa?(bg:, large: false) threshold = large ? 3.0 : 4.5 contrast_ratio(coerce_color(bg)) >= threshold end |
#wcag_aaa?(bg:, large: false) ⇒ Boolean
Check whether contrast against a background meets WCAG AAA.
415 416 417 418 |
# File 'lib/philiprehberger/color_convert/color.rb', line 415 def wcag_aaa?(bg:, large: false) threshold = large ? 4.5 : 7.0 contrast_ratio(coerce_color(bg)) >= threshold end |
#with_opacity(val) ⇒ Color
Return a new Color with the given opacity.
76 77 78 |
# File 'lib/philiprehberger/color_convert/color.rb', line 76 def with_opacity(val) self.class.new(@r, @g, @b, alpha: val) end |