Class: Abachrome::Outputs::CSS
- Inherits:
-
Object
- Object
- Abachrome::Outputs::CSS
- Defined in:
- lib/abachrome/outputs/css.rb
Class Method Summary collapse
- .format(color, gamut: nil, companding: nil) ⇒ Object
- .format_hex(color, gamut: nil, companding: nil) ⇒ Object
- .format_oklab(color, gamut: nil, companding: nil, precision: 3) ⇒ Object
- .format_rgb(color, gamut: nil, companding: nil) ⇒ Object
Class Method Details
.format(color, gamut: nil, companding: nil) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/abachrome/outputs/css.rb', line 9 def self.format(color, gamut: nil, companding: nil) rgb_color = color.to_rgb r, g, b = rgb_color.coordinates a = rgb_color.alpha # Apply gamut mapping if provided r, g, b = gamut.map([r, g, b]) if gamut # Apply companding if provided if companding r = companding.call(r) g = companding.call(g) b = companding.call(b) end # Convert to 8-bit values r = (r * 255).round g = (g * 255).round b = (b * 255).round # Format based on alpha value return Kernel.format("rgba(%d, %d, %d, %.3f)", r, g, b, a) unless a == AbcDecimal.new("1.0") return Kernel.format("#%02x%02x%02x", r, g, b) unless r == g && g == b # Use shortened hex format for grayscale hex = Kernel.format("%02x", r) "##{hex}#{hex}#{hex}" end |
.format_hex(color, gamut: nil, companding: nil) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/abachrome/outputs/css.rb', line 38 def self.format_hex(color, gamut: nil, companding: nil) rgb_color = color.to_rgb r, g, b = rgb_color.coordinates a = rgb_color.alpha # Apply gamut mapping if provided r, g, b = gamut.map([r, g, b]) if gamut # Apply companding if provided if companding r = companding.call(r) g = companding.call(g) b = companding.call(b) end r = (r * 255).round g = (g * 255).round b = (b * 255).round if a == AbcDecimal.new("1.0") Kernel.format("#%02x%02x%02x", r, g, b) else a = (a * 255).round Kernel.format("#%02x%02x%02x%02x", r, g, b, a) end end |
.format_oklab(color, gamut: nil, companding: nil, precision: 3) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/abachrome/outputs/css.rb', line 91 def self.format_oklab(color, gamut: nil, companding: nil, precision: 3) oklab_color = color.to_oklab l, a, b = oklab_color.coordinates alpha = oklab_color.alpha # Apply gamut mapping if provided l, a, b = gamut.map([l, a, b]) if gamut # Apply companding if provided if companding l = companding.call(l) a = companding.call(a) b = companding.call(b) end # Format with appropriate precision format_string = "%.#{precision}f %.#{precision}f %.#{precision}f" if alpha == AbcDecimal.new("1.0") Kernel.format("oklab(#{format_string})", l, a, b) else Kernel.format("oklab(#{format_string} / %.#{precision}f)", l, a, b, alpha) end end |
.format_rgb(color, gamut: nil, companding: nil) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/abachrome/outputs/css.rb', line 65 def self.format_rgb(color, gamut: nil, companding: nil) rgb_color = color.to_rgb r, g, b = rgb_color.coordinates a = rgb_color.alpha # Apply gamut mapping if provided r, g, b = gamut.map([r, g, b]) if gamut # Apply companding if provided if companding r = companding.call(r) g = companding.call(g) b = companding.call(b) end r = (r * 255).round g = (g * 255).round b = (b * 255).round if a == AbcDecimal.new("1.0") Kernel.format("rgb(%d, %d, %d)", r, g, b) else Kernel.format("rgba(%d, %d, %d, %.3f)", r, g, b, a) end end |