tinct 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/integer.rb +5 -0
- data/lib/string.rb +5 -0
- data/lib/tinct.rb +255 -0
- metadata +46 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 5fca6977fef6bdfd22b20e0e608f040c9d9e7ca24206a4b272f5c9bba8095207
|
|
4
|
+
data.tar.gz: 2da16913a8af3fcad8584285fdf739d4e12462983a48b7729e92ca34d61198d2
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 3446fff8655d035c6a63de18d1ed1c11ede475fdab1e8b8d60cef44ee50ff60f4e0e33bbd4988b4e9c47da128b418e86e0e16e0c4428b344bfd345d13791a27d
|
|
7
|
+
data.tar.gz: c7846a3aa20c82fade120750899c23da02176ffbd0686cd145c794a804fc18c97b084ff904a1feedc10b69a8e73cc17a9080562f7d5978105331d685116e8f57
|
data/lib/integer.rb
ADDED
data/lib/string.rb
ADDED
data/lib/tinct.rb
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
class Tinct
|
|
2
|
+
VERSION = '0.0.1'.freeze
|
|
3
|
+
|
|
4
|
+
attr_accessor :red, :green, :blue, :alpha
|
|
5
|
+
|
|
6
|
+
def initialize(red = 0, green = 0, blue = 0, alpha = 1)
|
|
7
|
+
@red = red.to_f.clamp(0.0, 1.0)
|
|
8
|
+
@green = green.to_f.clamp(0.0, 1.0)
|
|
9
|
+
@blue = blue.to_f.clamp(0.0, 1.0)
|
|
10
|
+
@alpha = alpha.to_f.clamp(0.0, 1.0)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def hue
|
|
14
|
+
max = [@red, @green, @blue].max
|
|
15
|
+
min = [@red, @green, @blue].min
|
|
16
|
+
delta = max - min
|
|
17
|
+
return 0 if delta == 0
|
|
18
|
+
return (((@green - @blue) / delta) % 6) / 6 if max == @red
|
|
19
|
+
return (((@blue - @red) / delta) + 2) / 6 if max == @green
|
|
20
|
+
return (((@red - @green) / delta) + 4) / 6
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def hue=(hue)
|
|
24
|
+
self.hue = Tinct.hsl(hue, saturation, lightness, alpha).hue
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def saturation
|
|
28
|
+
max = [@red, @green, @blue].max
|
|
29
|
+
min = [@red, @green, @blue].min
|
|
30
|
+
delta = max - min
|
|
31
|
+
l = (max + min / 2)
|
|
32
|
+
return 0 if max == min
|
|
33
|
+
l < 0.5 ? (max - min) / (max + min) : (max - min) / (2 - max - min)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def saturation=(saturation)
|
|
37
|
+
self.saturation = Tinct.hsl(hue, saturation, lightness, alpha).saturation
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def lightness
|
|
41
|
+
max = [@red, @green, @blue].max
|
|
42
|
+
min = [@red, @green, @blue].min
|
|
43
|
+
(max + min) / 2
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def lightness=(lightness)
|
|
47
|
+
self.lightness = Tinct.hsl(hue, saturation, lightness, alpha).lightness
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def value
|
|
51
|
+
[@red, @green, @blue].max
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def value=(value)
|
|
55
|
+
self.value = Tinct.hsv(hue, saturation, value, alpha).value
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def brightness
|
|
59
|
+
value
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def brightness=(brightness)
|
|
63
|
+
self.value = brightness
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def cyan
|
|
67
|
+
k = key
|
|
68
|
+
(1 - @red - k) / (1 - k)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def cyan=(cyan)
|
|
72
|
+
cyan = cyan.to_f.clamp(0.0, 1.0)
|
|
73
|
+
self.red = (1 - cyan) * (1 - key)
|
|
74
|
+
cyan
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def magenta
|
|
78
|
+
k = key
|
|
79
|
+
(1 - @green - k) / (1 - k)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def magenta=(magenta)
|
|
83
|
+
magenta = magenta.to_f.clamp(0.0, 1.0)
|
|
84
|
+
self.green = (1 - magenta) * (1 - key)
|
|
85
|
+
magenta
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def yellow
|
|
89
|
+
k = key
|
|
90
|
+
(1 - @blue - k) / (1 - k)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def yellow=(yellow)
|
|
94
|
+
yellow = yellow.to_f.clamp(0.0, 1.0)
|
|
95
|
+
self.blue = (1 - yellow) * (1 - key)
|
|
96
|
+
yellow
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def key
|
|
100
|
+
1 - [@red, @green, @blue].max
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def key=(key)
|
|
104
|
+
key = key.to_f.clamp(0.0, 1.0)
|
|
105
|
+
self.red = (1 - cyan) * (1 - key)
|
|
106
|
+
self.green = (1 - magenta) * (1 - key)
|
|
107
|
+
self.blue = (1 - yellow) * (1 - key)
|
|
108
|
+
key
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def self.hsl(hue = 0, saturation = 0, lightness = 0, alpha = 255)
|
|
112
|
+
hue = hue.to_f.clamp(0.0, 1.0) * 360
|
|
113
|
+
saturation = saturation.to_f.clamp(0.0, 1.0)
|
|
114
|
+
lightness = lightness.to_f.clamp(0.0, 1.0)
|
|
115
|
+
alpha = alpha.to_f.clamp(0.0, 1.0)
|
|
116
|
+
c = (1 - (2 * lightness - 1).abs) * saturation
|
|
117
|
+
x = c * (1 - ((hue / 60) % 2 - 1).abs)
|
|
118
|
+
m = lightness - c / 2
|
|
119
|
+
cxm(hue, c, x, m, alpha)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def self.hsv(hue = 0, saturation = 0, value = 1, alpha = 1)
|
|
123
|
+
hue = hue.to_f.clamp(0.0, 1.0) * 360
|
|
124
|
+
saturation = saturation.to_f.clamp(0.0, 1.0)
|
|
125
|
+
value = value.to_f.clamp(0.0, 1.0)
|
|
126
|
+
alpha = alpha.to_f.clamp(0.0, 1.0)
|
|
127
|
+
c = value * saturation
|
|
128
|
+
x = c * (1 - ((hue / 60) % 2 - 1).abs)
|
|
129
|
+
m = value - c
|
|
130
|
+
cxm(hue, c, x, m, alpha)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def self.hsb(hue = 0, saturation = 0, brightness = 1, alpha = 1)
|
|
134
|
+
self.hsv(hue, saturation, brightness, alpha)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def self.cxm(hue, c, x, m, alpha)
|
|
138
|
+
r_prime, g_prime, b_prime = 0, 0, 0
|
|
139
|
+
if hue < 60
|
|
140
|
+
r_prime, g_prime = c, x
|
|
141
|
+
elsif hue < 120
|
|
142
|
+
r_prime, g_prime = x, c
|
|
143
|
+
elsif hue < 180
|
|
144
|
+
g_prime, b_prime = c, x
|
|
145
|
+
elsif hue < 240
|
|
146
|
+
g_prime, b_prime = x, c
|
|
147
|
+
elsif hue < 300
|
|
148
|
+
r_prime, b_prime = x, c
|
|
149
|
+
else
|
|
150
|
+
r_prime, b_prime = c, x
|
|
151
|
+
end
|
|
152
|
+
Tinct.new(r_prime + m, g_prime + m, b_prime + m, alpha)
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
private_class_method :cxm
|
|
156
|
+
|
|
157
|
+
def self.rgb(red = 0, green = 0, blue = 0, alpha = 1)
|
|
158
|
+
Tinct.new(red, green, blue, alpha)
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def self.cmyk(cyan = 0, magenta = 0, yellow = 0, key = 0, alpha = 1)
|
|
162
|
+
cyan = cyan.to_f.clamp(0.0, 1.0)
|
|
163
|
+
magenta = magenta.to_f.clamp(0.0, 1.0)
|
|
164
|
+
yellow = yellow.to_f.clamp(0.0, 1.0)
|
|
165
|
+
key = key.to_f.clamp(0.0, 1.0)
|
|
166
|
+
alpha = alpha.to_f.clamp(0.0, 1.0)
|
|
167
|
+
Tinct.new(
|
|
168
|
+
(1 - cyan) * (1 - key),
|
|
169
|
+
(1 - magenta) * (1 - key),
|
|
170
|
+
(1 - yellow) * (1 - key),
|
|
171
|
+
alpha
|
|
172
|
+
)
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
def self.from_i(i, rgba = false)
|
|
176
|
+
i = i.to_i
|
|
177
|
+
alpha = 0
|
|
178
|
+
if rgba
|
|
179
|
+
alpha = i % 256 / 255.0
|
|
180
|
+
i /= 256
|
|
181
|
+
end
|
|
182
|
+
blue = i % 256 / 255.0
|
|
183
|
+
i /= 256
|
|
184
|
+
green = i % 256 / 255.0
|
|
185
|
+
i /= 256
|
|
186
|
+
red = i % 256 / 255.0
|
|
187
|
+
i /= 256
|
|
188
|
+
alpha = i unless rgba
|
|
189
|
+
Tinct.new(red, green, blue, alpha)
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def self.from_s(s)
|
|
193
|
+
s = s.rjust(6, '0')
|
|
194
|
+
s = 'ff' + s if s.length == 6
|
|
195
|
+
self.from_i(s.to_s.to_i(16))
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
def to_i(rgba = false)
|
|
199
|
+
i = 0
|
|
200
|
+
i = @alpha * 255 unless rgba
|
|
201
|
+
i *= 256
|
|
202
|
+
i += @red * 255
|
|
203
|
+
i *= 256
|
|
204
|
+
i += @green * 255
|
|
205
|
+
i *= 256
|
|
206
|
+
i += blue * 255
|
|
207
|
+
if rgba
|
|
208
|
+
i *= 256
|
|
209
|
+
i += @alpha * 255
|
|
210
|
+
end
|
|
211
|
+
i.to_i
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
def to_s(include_alpha=false)
|
|
215
|
+
s = to_i.to_s(16).rjust(8, '0')
|
|
216
|
+
s = s[2..-1] unless include_alpha
|
|
217
|
+
s
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
def self.mix(a, b)
|
|
221
|
+
a.mix(b)
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
def mix(other)
|
|
225
|
+
dup.mix!(other)
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
def mix!(other)
|
|
229
|
+
self.red = (@red + other.red) / 2
|
|
230
|
+
self.green = (@green + other.green) / 2
|
|
231
|
+
self.blue = (@blue + other.blue) / 2
|
|
232
|
+
self.alpha = (@alpha + other.alpha) / 2
|
|
233
|
+
self
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
def lighten(percentage)
|
|
237
|
+
dup.lighten!(percentage)
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
def lighten!(percentage)
|
|
241
|
+
percentage = percentage.to_f.clamp(0.0, 1.0)
|
|
242
|
+
self.key = key + percentage * (1 - key)
|
|
243
|
+
self
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
def darken(percentage)
|
|
247
|
+
dup.darken!(percentage)
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
def darken!(percentage)
|
|
251
|
+
percentage = percentage.to_f.clamp(0.0, 1.0)
|
|
252
|
+
self.key = percentage * key
|
|
253
|
+
self
|
|
254
|
+
end
|
|
255
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: tinct
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- questionmarkexclamationpoint
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2019-05-23 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description:
|
|
14
|
+
email:
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- lib/integer.rb
|
|
20
|
+
- lib/string.rb
|
|
21
|
+
- lib/tinct.rb
|
|
22
|
+
homepage:
|
|
23
|
+
licenses:
|
|
24
|
+
- MIT
|
|
25
|
+
metadata: {}
|
|
26
|
+
post_install_message:
|
|
27
|
+
rdoc_options: []
|
|
28
|
+
require_paths:
|
|
29
|
+
- lib
|
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
31
|
+
requirements:
|
|
32
|
+
- - ">="
|
|
33
|
+
- !ruby/object:Gem::Version
|
|
34
|
+
version: '0'
|
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0'
|
|
40
|
+
requirements: []
|
|
41
|
+
rubyforge_project:
|
|
42
|
+
rubygems_version: 2.7.6
|
|
43
|
+
signing_key:
|
|
44
|
+
specification_version: 4
|
|
45
|
+
summary: A library that adds a simple color type.
|
|
46
|
+
test_files: []
|