Class: Tinct

Inherits:
Object
  • Object
show all
Defined in:
lib/tinct.rb

Constant Summary collapse

VERSION =
'0.0.2'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#alphaObject

Returns the value of attribute alpha.



4
5
6
# File 'lib/tinct.rb', line 4

def alpha
  @alpha
end

#blueObject

Returns the value of attribute blue.



4
5
6
# File 'lib/tinct.rb', line 4

def blue
  @blue
end

#greenObject

Returns the value of attribute green.



4
5
6
# File 'lib/tinct.rb', line 4

def green
  @green
end

#redObject

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



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/tinct.rb', line 166

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



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/tinct.rb', line 180

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



197
198
199
200
201
# File 'lib/tinct.rb', line 197

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



138
139
140
# File 'lib/tinct.rb', line 138

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



116
117
118
119
120
121
122
123
124
125
# File 'lib/tinct.rb', line 116

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



127
128
129
130
131
132
133
134
135
136
# File 'lib/tinct.rb', line 127

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



225
226
227
# File 'lib/tinct.rb', line 225

def self.mix(a, b)
  a.mix(b)
end

.rgb(red = 0, green = 0, blue = 0, alpha = 1) ⇒ Object



162
163
164
# File 'lib/tinct.rb', line 162

def self.rgb(red = 0, green = 0, blue = 0, alpha = 1)
  Tinct.new(red, green, blue, alpha)
end

Instance Method Details

#brightnessObject



63
64
65
# File 'lib/tinct.rb', line 63

def brightness
  value
end

#brightness=(brightness) ⇒ Object



67
68
69
# File 'lib/tinct.rb', line 67

def brightness=(brightness)
  self.value = brightness
end

#cyanObject



71
72
73
74
# File 'lib/tinct.rb', line 71

def cyan
  k = key
  (1 - @red - k) / (1 - k)
end

#cyan=(cyan) ⇒ Object



76
77
78
79
80
# File 'lib/tinct.rb', line 76

def cyan=(cyan)
  cyan = cyan.to_f.clamp(0.0, 1.0)
  self.red = (1 - cyan) * (1 - key)
  cyan
end

#darken(percentage) ⇒ Object



251
252
253
# File 'lib/tinct.rb', line 251

def darken(percentage)
  dup.darken!(percentage)
end

#darken!(percentage) ⇒ Object



255
256
257
258
259
# File 'lib/tinct.rb', line 255

def darken!(percentage)
  percentage = percentage.to_f.clamp(0.0, 1.0)
  self.key = percentage * key
  self
end

#hueObject



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
26
# File 'lib/tinct.rb', line 23

def hue=(hue)
  copy(Tinct.hsl(hue, saturation, lightness, alpha))
  hue
end

#keyObject



104
105
106
# File 'lib/tinct.rb', line 104

def key
  1 - [@red, @green, @blue].max
end

#key=(key) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/tinct.rb', line 108

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



241
242
243
# File 'lib/tinct.rb', line 241

def lighten(percentage)
  dup.lighten!(percentage)
end

#lighten!(percentage) ⇒ Object



245
246
247
248
249
# File 'lib/tinct.rb', line 245

def lighten!(percentage)
  percentage = percentage.to_f.clamp(0.0, 1.0)
  self.key = key + percentage * (1 - key)
  self
end

#lightnessObject



43
44
45
46
47
# File 'lib/tinct.rb', line 43

def lightness
  max = [@red, @green, @blue].max
  min = [@red, @green, @blue].min
  (max + min) / 2
end

#lightness=(lightness) ⇒ Object



49
50
51
52
# File 'lib/tinct.rb', line 49

def lightness=(lightness)
  copy(Tinct.hsl(hue, saturation, lightness, alpha))
  lightness
end

#magentaObject



82
83
84
85
# File 'lib/tinct.rb', line 82

def magenta
  k = key
  (1 - @green - k) / (1 - k)
end

#magenta=(magenta) ⇒ Object



87
88
89
90
91
# File 'lib/tinct.rb', line 87

def magenta=(magenta)
  magenta = magenta.to_f.clamp(0.0, 1.0)
  self.green = (1 - magenta) * (1 - key)
  magenta
end

#mix(other) ⇒ Object



229
230
231
# File 'lib/tinct.rb', line 229

def mix(other)
  dup.mix!(other)
end

#mix!(other) ⇒ Object



233
234
235
236
237
238
239
# File 'lib/tinct.rb', line 233

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

#saturationObject



28
29
30
31
32
33
34
35
36
# File 'lib/tinct.rb', line 28

def saturation
  max = [@red, @green, @blue].max
  min = [@red, @green, @blue].min
  delta = max - min
  total = max + min
  l = total / 2
  return 0 if delta == 0
  l < 0.5 ? delta / total : delta / (2 - max - min)
end

#saturation=(saturation) ⇒ Object



38
39
40
41
# File 'lib/tinct.rb', line 38

def saturation=(saturation)
  copy(Tinct.hsl(hue, saturation, lightness, alpha))
  saturation
end

#to_i(rgba = false) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/tinct.rb', line 203

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



219
220
221
222
223
# File 'lib/tinct.rb', line 219

def to_s(include_alpha=false)
  s = to_i.to_s(16).rjust(8, '0')
  s = s[2..-1] unless include_alpha
  s
end

#valueObject



54
55
56
# File 'lib/tinct.rb', line 54

def value
  [@red, @green, @blue].max
end

#value=(value) ⇒ Object



58
59
60
61
# File 'lib/tinct.rb', line 58

def value=(value)
  copy(Tinct.hsv(hue, saturation, value, alpha))
  value
end

#yellowObject



93
94
95
96
# File 'lib/tinct.rb', line 93

def yellow
  k = key
  (1 - @blue - k) / (1 - k)
end

#yellow=(yellow) ⇒ Object



98
99
100
101
102
# File 'lib/tinct.rb', line 98

def yellow=(yellow)
  yellow = yellow.to_f.clamp(0.0, 1.0)
  self.blue = (1 - yellow) * (1 - key)
  yellow
end