Class: Tinct
- Inherits:
-
Object
- Object
- Tinct
- Defined in:
- lib/tinct.rb
Constant Summary collapse
- VERSION =
'0.0.1'.freeze
Instance Attribute Summary collapse
-
#alpha ⇒ Object
Returns the value of attribute alpha.
-
#blue ⇒ Object
Returns the value of attribute blue.
-
#green ⇒ Object
Returns the value of attribute green.
-
#red ⇒ Object
Returns the value of attribute red.
Class Method Summary collapse
- .cmyk(cyan = 0, magenta = 0, yellow = 0, key = 0, alpha = 1) ⇒ Object
- .from_i(i, rgba = false) ⇒ Object
- .from_s(s) ⇒ Object
- .hsb(hue = 0, saturation = 0, brightness = 1, alpha = 1) ⇒ Object
- .hsl(hue = 0, saturation = 0, lightness = 0, alpha = 255) ⇒ Object
- .hsv(hue = 0, saturation = 0, value = 1, alpha = 1) ⇒ Object
- .mix(a, b) ⇒ Object
- .rgb(red = 0, green = 0, blue = 0, alpha = 1) ⇒ Object
Instance Method Summary collapse
- #brightness ⇒ Object
- #brightness=(brightness) ⇒ Object
- #cyan ⇒ Object
- #cyan=(cyan) ⇒ Object
- #darken(percentage) ⇒ Object
- #darken!(percentage) ⇒ Object
- #hue ⇒ Object
- #hue=(hue) ⇒ Object
-
#initialize(red = 0, green = 0, blue = 0, alpha = 1) ⇒ Tinct
constructor
A new instance of Tinct.
- #key ⇒ Object
- #key=(key) ⇒ Object
- #lighten(percentage) ⇒ Object
- #lighten!(percentage) ⇒ Object
- #lightness ⇒ Object
- #lightness=(lightness) ⇒ Object
- #magenta ⇒ Object
- #magenta=(magenta) ⇒ Object
- #mix(other) ⇒ Object
- #mix!(other) ⇒ Object
- #saturation ⇒ Object
- #saturation=(saturation) ⇒ Object
- #to_i(rgba = false) ⇒ Object
- #to_s(include_alpha = false) ⇒ Object
- #value ⇒ Object
- #value=(value) ⇒ Object
- #yellow ⇒ Object
- #yellow=(yellow) ⇒ Object
Constructor Details
#initialize(red = 0, green = 0, blue = 0, alpha = 1) ⇒ Tinct
Returns a new instance of Tinct.
6 7 8 9 10 11 |
# File 'lib/tinct.rb', line 6 def initialize(red = 0, green = 0, blue = 0, alpha = 1) @red = red.to_f.clamp(0.0, 1.0) @green = green.to_f.clamp(0.0, 1.0) @blue = blue.to_f.clamp(0.0, 1.0) @alpha = alpha.to_f.clamp(0.0, 1.0) end |
Instance Attribute Details
#alpha ⇒ Object
Returns the value of attribute alpha.
4 5 6 |
# File 'lib/tinct.rb', line 4 def alpha @alpha end |
#blue ⇒ Object
Returns the value of attribute blue.
4 5 6 |
# File 'lib/tinct.rb', line 4 def blue @blue end |
#green ⇒ Object
Returns the value of attribute green.
4 5 6 |
# File 'lib/tinct.rb', line 4 def green @green end |
#red ⇒ Object
Returns the value of attribute red.
4 5 6 |
# File 'lib/tinct.rb', line 4 def red @red end |
Class Method Details
.cmyk(cyan = 0, magenta = 0, yellow = 0, key = 0, alpha = 1) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/tinct.rb', line 161 def self.cmyk(cyan = 0, magenta = 0, yellow = 0, key = 0, alpha = 1) cyan = cyan.to_f.clamp(0.0, 1.0) magenta = magenta.to_f.clamp(0.0, 1.0) yellow = yellow.to_f.clamp(0.0, 1.0) key = key.to_f.clamp(0.0, 1.0) alpha = alpha.to_f.clamp(0.0, 1.0) Tinct.new( (1 - cyan) * (1 - key), (1 - magenta) * (1 - key), (1 - yellow) * (1 - key), alpha ) end |
.from_i(i, rgba = false) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/tinct.rb', line 175 def self.from_i(i, rgba = false) i = i.to_i alpha = 0 if rgba alpha = i % 256 / 255.0 i /= 256 end blue = i % 256 / 255.0 i /= 256 green = i % 256 / 255.0 i /= 256 red = i % 256 / 255.0 i /= 256 alpha = i unless rgba Tinct.new(red, green, blue, alpha) end |
.from_s(s) ⇒ Object
192 193 194 195 196 |
# File 'lib/tinct.rb', line 192 def self.from_s(s) s = s.rjust(6, '0') s = 'ff' + s if s.length == 6 self.from_i(s.to_s.to_i(16)) end |
.hsb(hue = 0, saturation = 0, brightness = 1, alpha = 1) ⇒ Object
133 134 135 |
# File 'lib/tinct.rb', line 133 def self.hsb(hue = 0, saturation = 0, brightness = 1, alpha = 1) self.hsv(hue, saturation, brightness, alpha) end |
.hsl(hue = 0, saturation = 0, lightness = 0, alpha = 255) ⇒ Object
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/tinct.rb', line 111 def self.hsl(hue = 0, saturation = 0, lightness = 0, alpha = 255) hue = hue.to_f.clamp(0.0, 1.0) * 360 saturation = saturation.to_f.clamp(0.0, 1.0) lightness = lightness.to_f.clamp(0.0, 1.0) alpha = alpha.to_f.clamp(0.0, 1.0) c = (1 - (2 * lightness - 1).abs) * saturation x = c * (1 - ((hue / 60) % 2 - 1).abs) m = lightness - c / 2 cxm(hue, c, x, m, alpha) end |
.hsv(hue = 0, saturation = 0, value = 1, alpha = 1) ⇒ Object
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/tinct.rb', line 122 def self.hsv(hue = 0, saturation = 0, value = 1, alpha = 1) hue = hue.to_f.clamp(0.0, 1.0) * 360 saturation = saturation.to_f.clamp(0.0, 1.0) value = value.to_f.clamp(0.0, 1.0) alpha = alpha.to_f.clamp(0.0, 1.0) c = value * saturation x = c * (1 - ((hue / 60) % 2 - 1).abs) m = value - c cxm(hue, c, x, m, alpha) end |
.mix(a, b) ⇒ Object
220 221 222 |
# File 'lib/tinct.rb', line 220 def self.mix(a, b) a.mix(b) end |
Instance Method Details
#brightness ⇒ Object
58 59 60 |
# File 'lib/tinct.rb', line 58 def brightness value end |
#brightness=(brightness) ⇒ Object
62 63 64 |
# File 'lib/tinct.rb', line 62 def brightness=(brightness) self.value = brightness end |
#cyan ⇒ Object
66 67 68 69 |
# File 'lib/tinct.rb', line 66 def cyan k = key (1 - @red - k) / (1 - k) end |
#cyan=(cyan) ⇒ Object
71 72 73 74 75 |
# File 'lib/tinct.rb', line 71 def cyan=(cyan) cyan = cyan.to_f.clamp(0.0, 1.0) self.red = (1 - cyan) * (1 - key) cyan end |
#darken(percentage) ⇒ Object
246 247 248 |
# File 'lib/tinct.rb', line 246 def darken(percentage) dup.darken!(percentage) end |
#darken!(percentage) ⇒ Object
250 251 252 253 254 |
# File 'lib/tinct.rb', line 250 def darken!(percentage) percentage = percentage.to_f.clamp(0.0, 1.0) self.key = percentage * key self end |
#hue ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/tinct.rb', line 13 def hue max = [@red, @green, @blue].max min = [@red, @green, @blue].min delta = max - min return 0 if delta == 0 return (((@green - @blue) / delta) % 6) / 6 if max == @red return (((@blue - @red) / delta) + 2) / 6 if max == @green return (((@red - @green) / delta) + 4) / 6 end |
#hue=(hue) ⇒ Object
23 24 25 |
# File 'lib/tinct.rb', line 23 def hue=(hue) self.hue = Tinct.hsl(hue, saturation, lightness, alpha).hue end |
#key ⇒ Object
99 100 101 |
# File 'lib/tinct.rb', line 99 def key 1 - [@red, @green, @blue].max end |
#key=(key) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/tinct.rb', line 103 def key=(key) key = key.to_f.clamp(0.0, 1.0) self.red = (1 - cyan) * (1 - key) self.green = (1 - magenta) * (1 - key) self.blue = (1 - yellow) * (1 - key) key end |
#lighten(percentage) ⇒ Object
236 237 238 |
# File 'lib/tinct.rb', line 236 def lighten(percentage) dup.lighten!(percentage) end |
#lighten!(percentage) ⇒ Object
240 241 242 243 244 |
# File 'lib/tinct.rb', line 240 def lighten!(percentage) percentage = percentage.to_f.clamp(0.0, 1.0) self.key = key + percentage * (1 - key) self end |
#lightness ⇒ Object
40 41 42 43 44 |
# File 'lib/tinct.rb', line 40 def lightness max = [@red, @green, @blue].max min = [@red, @green, @blue].min (max + min) / 2 end |
#lightness=(lightness) ⇒ Object
46 47 48 |
# File 'lib/tinct.rb', line 46 def lightness=(lightness) self.lightness = Tinct.hsl(hue, saturation, lightness, alpha).lightness end |
#magenta ⇒ Object
77 78 79 80 |
# File 'lib/tinct.rb', line 77 def magenta k = key (1 - @green - k) / (1 - k) end |
#magenta=(magenta) ⇒ Object
82 83 84 85 86 |
# File 'lib/tinct.rb', line 82 def magenta=(magenta) magenta = magenta.to_f.clamp(0.0, 1.0) self.green = (1 - magenta) * (1 - key) magenta end |
#mix(other) ⇒ Object
224 225 226 |
# File 'lib/tinct.rb', line 224 def mix(other) dup.mix!(other) end |
#mix!(other) ⇒ Object
228 229 230 231 232 233 234 |
# File 'lib/tinct.rb', line 228 def mix!(other) self.red = (@red + other.red) / 2 self.green = (@green + other.green) / 2 self.blue = (@blue + other.blue) / 2 self.alpha = (@alpha + other.alpha) / 2 self end |
#saturation ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/tinct.rb', line 27 def saturation max = [@red, @green, @blue].max min = [@red, @green, @blue].min delta = max - min l = (max + min / 2) return 0 if max == min l < 0.5 ? (max - min) / (max + min) : (max - min) / (2 - max - min) end |
#saturation=(saturation) ⇒ Object
36 37 38 |
# File 'lib/tinct.rb', line 36 def saturation=(saturation) self.saturation = Tinct.hsl(hue, saturation, lightness, alpha).saturation end |
#to_i(rgba = false) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/tinct.rb', line 198 def to_i(rgba = false) i = 0 i = @alpha * 255 unless rgba i *= 256 i += @red * 255 i *= 256 i += @green * 255 i *= 256 i += blue * 255 if rgba i *= 256 i += @alpha * 255 end i.to_i end |
#to_s(include_alpha = false) ⇒ Object
214 215 216 217 218 |
# File 'lib/tinct.rb', line 214 def to_s(include_alpha=false) s = to_i.to_s(16).rjust(8, '0') s = s[2..-1] unless include_alpha s end |
#value ⇒ Object
50 51 52 |
# File 'lib/tinct.rb', line 50 def value [@red, @green, @blue].max end |
#value=(value) ⇒ Object
54 55 56 |
# File 'lib/tinct.rb', line 54 def value=(value) self.value = Tinct.hsv(hue, saturation, value, alpha).value end |
#yellow ⇒ Object
88 89 90 91 |
# File 'lib/tinct.rb', line 88 def yellow k = key (1 - @blue - k) / (1 - k) end |
#yellow=(yellow) ⇒ Object
93 94 95 96 97 |
# File 'lib/tinct.rb', line 93 def yellow=(yellow) yellow = yellow.to_f.clamp(0.0, 1.0) self.blue = (1 - yellow) * (1 - key) yellow end |