rgbcell 0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +119 -0
- data/lib/rgbcell.rb +574 -0
- metadata +46 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: bb2bf06b2bb1f5162249b2a5e02d1ec58ad4b35a7c4f172c30a3302f6ec36f99
|
4
|
+
data.tar.gz: e3df58f700117a89c5e2183ec36f90859986f0567888923948439a49d1f65096
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5182f8c620e5bd05652efbc95c4cc75167a323b316e154c50fe4162a81758873e1157b92640a51cc8fd2cd978bf83744fd81c4f759b8337b2be28b869aacf82b
|
7
|
+
data.tar.gz: d9aac83ce02fa07b1c92104cd8ee21e7fcd4c10c8bbd7da24a9fde624aa4e1b9016d0a2970d485148d72af9e2dbff20c8c8317631ed9cdbb12dcde24223c2721
|
data/README.md
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
# RGBCell
|
2
|
+
|
3
|
+
RGBCell represents a color as the coordinates in the x-y-z coordinate system.
|
4
|
+
Each of the three component color elements, red, green, and blue, have a float
|
5
|
+
value from 0 to 256.
|
6
|
+
|
7
|
+
Creating an object without any params creates black:
|
8
|
+
|
9
|
+
1. require 'rgbcell'
|
10
|
+
2. color = RGBCell.new
|
11
|
+
3. color.to_s #=> #000000
|
12
|
+
4. color.red #=> 0.0
|
13
|
+
5. color.green #=> 0.0
|
14
|
+
6. color.blue #=> 0.0
|
15
|
+
|
16
|
+
Line 1 loads the RGBCell module.
|
17
|
+
Line 2 create an RGBCell object. Because there are no parameters
|
18
|
+
given, the object represents black. Line 3 show the
|
19
|
+
stringification of the color such as might be used in CSS. Lines
|
20
|
+
4-6 show the numeric values of the three component
|
21
|
+
colors, which are stored as floats.
|
22
|
+
|
23
|
+
There are several ways to indicate the color in RGBCell::new. One way is to give
|
24
|
+
its three components as numeric values.
|
25
|
+
|
26
|
+
color = RGBCell.new(127, 255, 0)
|
27
|
+
color.to_s #=> #7fff00
|
28
|
+
|
29
|
+
You can indicate the color using hex format:
|
30
|
+
|
31
|
+
color = RGBCell.new('#7fff00')
|
32
|
+
color.to_s #=> #7fff00
|
33
|
+
color.red #=> 127.0
|
34
|
+
color.green #=> 255.0
|
35
|
+
color.blue #=> 0.0
|
36
|
+
|
37
|
+
You can give one of the HTML named colors:
|
38
|
+
|
39
|
+
color = RGBCell.new('purple')
|
40
|
+
color.to_s #=> #800080
|
41
|
+
color.red #=> 128.0
|
42
|
+
color.green #=> 0.0
|
43
|
+
color.blue #=> 128.0
|
44
|
+
|
45
|
+
Finally, you can use 'random' to get a random color:
|
46
|
+
|
47
|
+
color = RGBCell.new('random')
|
48
|
+
color.to_s #=> #dbd25b
|
49
|
+
color.red #=> 219.0
|
50
|
+
color.green #=> 210.0
|
51
|
+
color.blue #=> 91.0
|
52
|
+
|
53
|
+
You can also set each component color individually. Notice in this example that
|
54
|
+
a component integer value is converted to float.
|
55
|
+
|
56
|
+
color = RGBCell.new()
|
57
|
+
color.red = 60 #=> 60.0
|
58
|
+
color.green = 230 #=> 210.0
|
59
|
+
color.blue = 91.005 #=> 91.0005
|
60
|
+
color.to_s #=> #3ce65b
|
61
|
+
|
62
|
+
## Distance
|
63
|
+
|
64
|
+
Because every color is a set of coordinates, the difference between two colors
|
65
|
+
can be expressed as a distance. The distance is always zero or a positive
|
66
|
+
number. For example, the following example gives the distance between red and
|
67
|
+
chartreuse.
|
68
|
+
|
69
|
+
red = RGBCell.new('red')
|
70
|
+
chartreuse = RGBCell.new('chartreuse')
|
71
|
+
red.distance(chartreuse) #=> 285.3226244096321
|
72
|
+
|
73
|
+
The same thing can be expressed with the minus operator.
|
74
|
+
|
75
|
+
puts red - chartreuse #=> 285.3226244096321
|
76
|
+
|
77
|
+
The second color does not have to be an RGBCell object; it only needs to be an
|
78
|
+
expression that can be used to create an RGBCell object.
|
79
|
+
|
80
|
+
puts red - 'green' #=> 360.62445840513925
|
81
|
+
|
82
|
+
## Average
|
83
|
+
|
84
|
+
Two or more colors can be averaged by calculating the midpoint of their
|
85
|
+
coordinates using the `average` method. The result of the average is itself an
|
86
|
+
RGBCell object.
|
87
|
+
|
88
|
+
red = RGBCell.new('red')
|
89
|
+
tomato = RGBCell.new('tomato')
|
90
|
+
avg = red.average(tomato)
|
91
|
+
puts avg.class #=> RGBCell
|
92
|
+
puts avg.to_s #=> #ff3100
|
93
|
+
|
94
|
+
You can pass in more than one color for averaging. You can use RGBCell objects
|
95
|
+
or any object that can be used to create an RGBCell object.
|
96
|
+
|
97
|
+
tomato = RGBCell.new('tomato')
|
98
|
+
puts tomato.average('orange', 'olive') #=> #d48200
|
99
|
+
|
100
|
+
If you prefer not to create any colors at all, you can use the class method.
|
101
|
+
|
102
|
+
puts RGBCell.average('tomato', 'orange', 'olive') #=> #d48200
|
103
|
+
|
104
|
+
## Install
|
105
|
+
|
106
|
+
```
|
107
|
+
gem install rgbcube
|
108
|
+
```
|
109
|
+
|
110
|
+
## Author
|
111
|
+
|
112
|
+
Mike O'Sullivan
|
113
|
+
mike@idocs.com
|
114
|
+
|
115
|
+
## History
|
116
|
+
|
117
|
+
| version | date | notes |
|
118
|
+
|---------|---------------|-------------------------------------------------------|
|
119
|
+
| 0.5 | June 25, 2020 | Initial upload. |
|
data/lib/rgbcell.rb
ADDED
@@ -0,0 +1,574 @@
|
|
1
|
+
#===============================================================================
|
2
|
+
# RGBCell
|
3
|
+
#
|
4
|
+
|
5
|
+
# An RGBCell object represents a single color in the red-green-blue color
|
6
|
+
# scheme, such as is used in HTML or CSS.
|
7
|
+
|
8
|
+
class RGBCell
|
9
|
+
# version
|
10
|
+
VERSION = '0.5'
|
11
|
+
|
12
|
+
#---------------------------------------------------------------------------
|
13
|
+
# initialize
|
14
|
+
#
|
15
|
+
|
16
|
+
# Initializes a new RGBCell object. The params can be in one of several
|
17
|
+
# forms.
|
18
|
+
#
|
19
|
+
# - three numbers
|
20
|
+
# RGBCell.new(127, 255, 0)
|
21
|
+
#
|
22
|
+
# - a named color
|
23
|
+
# RGBCell.new('orange')
|
24
|
+
#
|
25
|
+
# - a hexidecimal expression
|
26
|
+
# RGBCell.new('#7fff00')
|
27
|
+
#
|
28
|
+
# - a random color
|
29
|
+
# RGBCell.new('random')
|
30
|
+
#
|
31
|
+
# - no param returns black
|
32
|
+
# RGBCell.new
|
33
|
+
|
34
|
+
def initialize(*opts)
|
35
|
+
# $tm.hrm
|
36
|
+
|
37
|
+
# no opts: set to black
|
38
|
+
if opts.length == 0
|
39
|
+
@red = 0.0
|
40
|
+
@green = 0.0
|
41
|
+
@blue = 0.0
|
42
|
+
|
43
|
+
# 1 opt
|
44
|
+
elsif opts.length == 1
|
45
|
+
# array
|
46
|
+
if opts[0].is_a?(Array)
|
47
|
+
@red = opts[0][0].to_f
|
48
|
+
@green = opts[0][1].to_f
|
49
|
+
@blue = opts[0][2].to_f
|
50
|
+
|
51
|
+
# random
|
52
|
+
elsif opts[0] == 'random'
|
53
|
+
randomize()
|
54
|
+
|
55
|
+
# named
|
56
|
+
elsif coords = NAMES[opts[0]]
|
57
|
+
@red = coords[0].to_f
|
58
|
+
@green = coords[1].to_f
|
59
|
+
@blue = coords[2].to_f
|
60
|
+
|
61
|
+
# hex
|
62
|
+
elsif opts[0].match(/\A\#?[a-f0-9]{6,}\z/mu)
|
63
|
+
rgbs = opts[0]
|
64
|
+
rgbs = rgbs.sub(/\A\#/mu, '')
|
65
|
+
@red = rgbs[0..1].hex
|
66
|
+
@green = rgbs[2..3].hex
|
67
|
+
@blue = rgbs[4..5].hex
|
68
|
+
|
69
|
+
# else unknown opt
|
70
|
+
else
|
71
|
+
raise 'unknown-opt: ' + opts[0].to_s
|
72
|
+
end
|
73
|
+
|
74
|
+
# 3 opts
|
75
|
+
elsif opts.length == 3
|
76
|
+
@red = opts[0].to_f
|
77
|
+
@green = opts[1].to_f
|
78
|
+
@blue = opts[2].to_f
|
79
|
+
|
80
|
+
# any other number of options is invalid
|
81
|
+
else
|
82
|
+
raise 'invalid-initialize-options-length: ' + opts.length.to_s
|
83
|
+
end
|
84
|
+
end
|
85
|
+
#
|
86
|
+
# initialize
|
87
|
+
#---------------------------------------------------------------------------
|
88
|
+
|
89
|
+
|
90
|
+
#---------------------------------------------------------------------------
|
91
|
+
# readers
|
92
|
+
#
|
93
|
+
|
94
|
+
# The value of the red component.
|
95
|
+
attr_reader :red
|
96
|
+
|
97
|
+
# The value of the green component.
|
98
|
+
attr_reader :green
|
99
|
+
|
100
|
+
# The value of the blue component.
|
101
|
+
attr_reader :blue
|
102
|
+
|
103
|
+
#
|
104
|
+
# readers
|
105
|
+
#---------------------------------------------------------------------------
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
#---------------------------------------------------------------------------
|
110
|
+
# obj_or_new
|
111
|
+
#
|
112
|
+
|
113
|
+
# If the given object is an EGBCell object, then it is returned. Otherwise
|
114
|
+
# the object is used to create an RGBCell object.
|
115
|
+
def self.obj_or_new(*positions)
|
116
|
+
if positions[0].is_a?(RGBCell)
|
117
|
+
return positions[0]
|
118
|
+
else
|
119
|
+
return self.new(*positions)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
#
|
124
|
+
# obj_or_new
|
125
|
+
#---------------------------------------------------------------------------
|
126
|
+
|
127
|
+
|
128
|
+
#---------------------------------------------------------------------------
|
129
|
+
# red=, green=, blue=
|
130
|
+
#
|
131
|
+
|
132
|
+
# Sets the red coordinate. Converts the given number to a float.
|
133
|
+
def red=(coord)
|
134
|
+
@red = coord.to_f
|
135
|
+
end
|
136
|
+
|
137
|
+
# Sets the greens coordinate. Converts the given number to a float.
|
138
|
+
def green=(coord)
|
139
|
+
@green = coord.to_f
|
140
|
+
end
|
141
|
+
|
142
|
+
# Sets the blue coordinate. Converts the given number to a float.
|
143
|
+
def blue=(coord)
|
144
|
+
@blue = coord.to_f
|
145
|
+
end
|
146
|
+
|
147
|
+
#
|
148
|
+
# red=, green=, blue=
|
149
|
+
#---------------------------------------------------------------------------
|
150
|
+
|
151
|
+
|
152
|
+
#---------------------------------------------------------------------------
|
153
|
+
# positions
|
154
|
+
#
|
155
|
+
|
156
|
+
# Returns an array of the three component coordinates.
|
157
|
+
def positions()
|
158
|
+
return [@red, @green, @blue]
|
159
|
+
end
|
160
|
+
#
|
161
|
+
# positions
|
162
|
+
#---------------------------------------------------------------------------
|
163
|
+
|
164
|
+
|
165
|
+
#---------------------------------------------------------------------------
|
166
|
+
# randomize
|
167
|
+
#
|
168
|
+
|
169
|
+
# Sets the object to a random color.
|
170
|
+
def randomize
|
171
|
+
@red = self.class.rand_255().to_f
|
172
|
+
@green = self.class.rand_255().to_f
|
173
|
+
@blue = self.class.rand_255().to_f
|
174
|
+
end
|
175
|
+
|
176
|
+
#
|
177
|
+
# randomize
|
178
|
+
#---------------------------------------------------------------------------
|
179
|
+
|
180
|
+
|
181
|
+
#---------------------------------------------------------------------------
|
182
|
+
# average
|
183
|
+
#
|
184
|
+
|
185
|
+
# Returns a color representing the midpoint between the object's color and
|
186
|
+
# the other given colors.
|
187
|
+
def average(*others)
|
188
|
+
# $tm.hrm
|
189
|
+
r_sum = @red
|
190
|
+
g_sum = @green
|
191
|
+
b_sum = @blue
|
192
|
+
|
193
|
+
# loop through others
|
194
|
+
others.each do |other|
|
195
|
+
other = self.class.obj_or_new(other)
|
196
|
+
r_sum += other.red
|
197
|
+
g_sum += other.green
|
198
|
+
b_sum += other.blue
|
199
|
+
end
|
200
|
+
|
201
|
+
# total
|
202
|
+
total = others.length + 1
|
203
|
+
|
204
|
+
# return
|
205
|
+
return self.class.new( r_sum/total, g_sum/total, b_sum/total )
|
206
|
+
end
|
207
|
+
|
208
|
+
#
|
209
|
+
# average
|
210
|
+
#---------------------------------------------------------------------------
|
211
|
+
|
212
|
+
|
213
|
+
#---------------------------------------------------------------------------
|
214
|
+
# self.average
|
215
|
+
#
|
216
|
+
|
217
|
+
# Returns the midpoint of the given colors expressed as a color.
|
218
|
+
def self.average(*colors)
|
219
|
+
base = self.obj_or_new(colors.shift)
|
220
|
+
return base.average(*colors)
|
221
|
+
end
|
222
|
+
|
223
|
+
#
|
224
|
+
# self.average
|
225
|
+
#---------------------------------------------------------------------------
|
226
|
+
|
227
|
+
|
228
|
+
#---------------------------------------------------------------------------
|
229
|
+
# to_s, to_str, +
|
230
|
+
#
|
231
|
+
|
232
|
+
# Returns the hexidecimal representation of the color.
|
233
|
+
def to_s
|
234
|
+
return to_str()
|
235
|
+
end
|
236
|
+
|
237
|
+
# Returns the hexidecimal representation of the color.
|
238
|
+
def to_str
|
239
|
+
rv = '#'
|
240
|
+
|
241
|
+
# loop through coordinates
|
242
|
+
positions.each do |pos|
|
243
|
+
rv += self.class.coord_to_hex(pos)
|
244
|
+
end
|
245
|
+
|
246
|
+
# return
|
247
|
+
return rv
|
248
|
+
end
|
249
|
+
|
250
|
+
# I don't remember what the point of this method was supposed to be.
|
251
|
+
# def +(val)
|
252
|
+
# return to_str() + val.to_str
|
253
|
+
# end
|
254
|
+
|
255
|
+
#
|
256
|
+
# to_s, to_str, +
|
257
|
+
#---------------------------------------------------------------------------
|
258
|
+
|
259
|
+
|
260
|
+
#---------------------------------------------------------------------------
|
261
|
+
# coord_to_hex
|
262
|
+
#
|
263
|
+
|
264
|
+
# Converts a color coordinate to hexidecimal.
|
265
|
+
def self.coord_to_hex(coord)
|
266
|
+
return coord.to_i.to_s(16).rjust(2, '0')
|
267
|
+
end
|
268
|
+
#
|
269
|
+
# coord_to_hex
|
270
|
+
#---------------------------------------------------------------------------
|
271
|
+
|
272
|
+
|
273
|
+
#---------------------------------------------------------------------------
|
274
|
+
# distance
|
275
|
+
#
|
276
|
+
|
277
|
+
# Returns the distance between the color and the given color. Always returns
|
278
|
+
# zero or a positive number.
|
279
|
+
def distance(*other)
|
280
|
+
# $tm.hrm
|
281
|
+
|
282
|
+
# ensure object
|
283
|
+
other = self.class.obj_or_new(*other)
|
284
|
+
|
285
|
+
# individual distances
|
286
|
+
red_d = (@red - other.red).abs2
|
287
|
+
green_d = (@green - other.green).abs2
|
288
|
+
blue_d = (@blue - other.blue).abs2
|
289
|
+
|
290
|
+
# rv
|
291
|
+
return Math.sqrt(red_d + green_d + blue_d).abs
|
292
|
+
end
|
293
|
+
#
|
294
|
+
# distance
|
295
|
+
#---------------------------------------------------------------------------
|
296
|
+
|
297
|
+
|
298
|
+
#---------------------------------------------------------------------------
|
299
|
+
# -
|
300
|
+
#
|
301
|
+
|
302
|
+
# Alias for RGBCell#distance.
|
303
|
+
def -(*other)
|
304
|
+
return distance(*other)
|
305
|
+
end
|
306
|
+
|
307
|
+
#
|
308
|
+
# -
|
309
|
+
#---------------------------------------------------------------------------
|
310
|
+
|
311
|
+
|
312
|
+
#---------------------------------------------------------------------------
|
313
|
+
# named colors
|
314
|
+
# https://www.w3.org/wiki/CSS/Properties/color/keywords
|
315
|
+
#
|
316
|
+
|
317
|
+
# A hash of official named HTML colors. For the full list of names, see
|
318
|
+
# https://www.w3.org/wiki/CSS/Properties/color/keywords
|
319
|
+
NAMES = {}
|
320
|
+
|
321
|
+
NAMES['aliceblue'] = [240, 248, 255]
|
322
|
+
NAMES['antiquewhite'] = [250, 235, 215]
|
323
|
+
NAMES['aqua'] = [0, 255, 255]
|
324
|
+
NAMES['aquamarine'] = [127, 255, 212]
|
325
|
+
NAMES['azure'] = [240, 255, 255]
|
326
|
+
NAMES['beige'] = [245, 245, 220]
|
327
|
+
NAMES['bisque'] = [255, 228, 196]
|
328
|
+
NAMES['black'] = [0, 0, 0]
|
329
|
+
NAMES['blanchedalmond'] = [255, 235, 205]
|
330
|
+
NAMES['blue'] = [0, 0, 255]
|
331
|
+
NAMES['blueviolet'] = [138, 43, 226]
|
332
|
+
NAMES['brown'] = [165, 42, 42]
|
333
|
+
NAMES['burlywood'] = [222, 184, 135]
|
334
|
+
NAMES['cadetblue'] = [95, 158, 160]
|
335
|
+
NAMES['chartreuse'] = [127, 255, 0]
|
336
|
+
NAMES['chocolate'] = [210, 105, 30]
|
337
|
+
NAMES['coral'] = [255, 127, 80]
|
338
|
+
NAMES['cornflowerblue'] = [100, 149, 237]
|
339
|
+
NAMES['cornsilk'] = [255, 248, 220]
|
340
|
+
NAMES['crimson'] = [220, 20, 60]
|
341
|
+
NAMES['cyan'] = [0, 255, 255]
|
342
|
+
NAMES['darkblue'] = [0, 0, 139]
|
343
|
+
NAMES['darkcyan'] = [0, 139, 139]
|
344
|
+
NAMES['darkgoldenrod'] = [184, 134, 11]
|
345
|
+
NAMES['darkgray'] = [169, 169, 169]
|
346
|
+
NAMES['darkgreen'] = [0, 100, 0]
|
347
|
+
NAMES['darkkhaki'] = [189, 183, 107]
|
348
|
+
NAMES['darkmagenta'] = [139, 0, 139]
|
349
|
+
NAMES['darkolivegreen'] = [85, 107, 47]
|
350
|
+
NAMES['darkorange'] = [255, 140, 0]
|
351
|
+
NAMES['darkorchid'] = [153, 50, 204]
|
352
|
+
NAMES['darkred'] = [139, 0, 0]
|
353
|
+
NAMES['darksalmon'] = [233, 150, 122]
|
354
|
+
NAMES['darkseagreen'] = [143, 188, 143]
|
355
|
+
NAMES['darkslateblue'] = [72, 61, 139]
|
356
|
+
NAMES['darkslategray'] = [47, 79, 79]
|
357
|
+
NAMES['darkturquoise'] = [0, 206, 209]
|
358
|
+
NAMES['darkviolet'] = [148, 0, 211]
|
359
|
+
NAMES['deeppink'] = [255, 20, 147]
|
360
|
+
NAMES['deepskyblue'] = [0, 191, 255]
|
361
|
+
NAMES['dimgray'] = [105, 105, 105]
|
362
|
+
NAMES['dodgerblue'] = [30, 144, 255]
|
363
|
+
NAMES['firebrick'] = [178, 34, 34]
|
364
|
+
NAMES['floralwhite'] = [255, 250, 240]
|
365
|
+
NAMES['forestgreen'] = [34, 139, 34]
|
366
|
+
NAMES['fuchsia'] = [255, 0, 255]
|
367
|
+
NAMES['gainsboro'] = [220, 220, 220]
|
368
|
+
NAMES['ghostwhite'] = [248, 248, 255]
|
369
|
+
NAMES['gold'] = [255, 215, 0]
|
370
|
+
NAMES['goldenrod'] = [218, 165, 32]
|
371
|
+
NAMES['gray'] = [128, 128, 128]
|
372
|
+
NAMES['green'] = [0, 128, 0]
|
373
|
+
NAMES['greenyellow'] = [173, 255, 47]
|
374
|
+
NAMES['honeydew'] = [240, 255, 240]
|
375
|
+
NAMES['hotpink'] = [255, 105, 180]
|
376
|
+
NAMES['indianred'] = [205, 92, 92]
|
377
|
+
NAMES['indigo'] = [75, 0, 130]
|
378
|
+
NAMES['ivory'] = [255, 255, 240]
|
379
|
+
NAMES['khaki'] = [240, 230, 140]
|
380
|
+
NAMES['lavender'] = [230, 230, 250]
|
381
|
+
NAMES['lavenderblush'] = [255, 240, 245]
|
382
|
+
NAMES['lawngreen'] = [124, 252, 0]
|
383
|
+
NAMES['lemonchiffon'] = [255, 250, 205]
|
384
|
+
NAMES['lightblue'] = [173, 216, 230]
|
385
|
+
NAMES['lightcoral'] = [240, 128, 128]
|
386
|
+
NAMES['lightcyan'] = [224, 255, 255]
|
387
|
+
NAMES['lightgoldenrodyellow'] = [250, 250, 210]
|
388
|
+
NAMES['lightgray'] = [211, 211, 211]
|
389
|
+
NAMES['lightgreen'] = [144, 238, 144]
|
390
|
+
NAMES['lightpink'] = [255, 182, 193]
|
391
|
+
NAMES['lightsalmon'] = [255, 160, 122]
|
392
|
+
NAMES['lightseagreen'] = [32, 178, 170]
|
393
|
+
NAMES['lightskyblue'] = [135, 206, 250]
|
394
|
+
NAMES['lightslategray'] = [119, 136, 153]
|
395
|
+
NAMES['lightsteelblue'] = [176, 196, 222]
|
396
|
+
NAMES['lightyellow'] = [255, 255, 224]
|
397
|
+
NAMES['lime'] = [0, 255, 0]
|
398
|
+
NAMES['limegreen'] = [50, 205, 50]
|
399
|
+
NAMES['linen'] = [250, 240, 230]
|
400
|
+
NAMES['magenta'] = [255, 0, 255]
|
401
|
+
NAMES['maroon'] = [128, 0, 0]
|
402
|
+
NAMES['mediumaquamarine'] = [102, 205, 170]
|
403
|
+
NAMES['mediumblue'] = [0, 0, 205]
|
404
|
+
NAMES['mediumorchid'] = [186, 85, 211]
|
405
|
+
NAMES['mediumpurple'] = [147, 112, 219]
|
406
|
+
NAMES['mediumseagreen'] = [60, 179, 113]
|
407
|
+
NAMES['mediumslateblue'] = [123, 104, 238]
|
408
|
+
NAMES['mediumspringgreen'] = [0, 250, 154]
|
409
|
+
NAMES['mediumturquoise'] = [72, 209, 204]
|
410
|
+
NAMES['mediumvioletred'] = [199, 21, 133]
|
411
|
+
NAMES['midnightblue'] = [25, 25, 112]
|
412
|
+
NAMES['mintcream'] = [245, 255, 250]
|
413
|
+
NAMES['mistyrose'] = [255, 228, 225]
|
414
|
+
NAMES['moccasin'] = [255, 228, 181]
|
415
|
+
NAMES['navajowhite'] = [255, 222, 173]
|
416
|
+
NAMES['navy'] = [0, 0, 128]
|
417
|
+
NAMES['oldlace'] = [253, 245, 230]
|
418
|
+
NAMES['olive'] = [128, 128, 0]
|
419
|
+
NAMES['olivedrab'] = [107, 142, 35]
|
420
|
+
NAMES['orange'] = [255, 165, 0]
|
421
|
+
NAMES['orangered'] = [255, 69, 0]
|
422
|
+
NAMES['orchid'] = [218, 112, 214]
|
423
|
+
NAMES['palegoldenrod'] = [238, 232, 170]
|
424
|
+
NAMES['palegreen'] = [152, 251, 152]
|
425
|
+
NAMES['paleturquoise'] = [175, 238, 238]
|
426
|
+
NAMES['palevioletred'] = [219, 112, 147]
|
427
|
+
NAMES['papayawhip'] = [255, 239, 213]
|
428
|
+
NAMES['peachpuff'] = [255, 218, 185]
|
429
|
+
NAMES['peru'] = [205, 133, 63]
|
430
|
+
NAMES['pink'] = [255, 192, 203]
|
431
|
+
NAMES['plum'] = [221, 160, 221]
|
432
|
+
NAMES['powderblue'] = [176, 224, 230]
|
433
|
+
NAMES['purple'] = [128, 0, 128]
|
434
|
+
NAMES['red'] = [255, 0, 0]
|
435
|
+
NAMES['rosybrown'] = [188, 143, 143]
|
436
|
+
NAMES['royalblue'] = [65, 105, 225]
|
437
|
+
NAMES['saddlebrown'] = [139, 69, 19]
|
438
|
+
NAMES['salmon'] = [250, 128, 114]
|
439
|
+
NAMES['sandybrown'] = [244, 164, 96]
|
440
|
+
NAMES['seagreen'] = [46, 139, 87]
|
441
|
+
NAMES['seashell'] = [255, 245, 238]
|
442
|
+
NAMES['sienna'] = [160, 82, 45]
|
443
|
+
NAMES['silver'] = [192, 192, 192]
|
444
|
+
NAMES['skyblue'] = [135, 206, 235]
|
445
|
+
NAMES['slateblue'] = [106, 90, 205]
|
446
|
+
NAMES['slategray'] = [112, 128, 144]
|
447
|
+
NAMES['snow'] = [255, 250, 250]
|
448
|
+
NAMES['springgreen'] = [0, 255, 127]
|
449
|
+
NAMES['steelblue'] = [70, 130, 180]
|
450
|
+
NAMES['tan'] = [210, 180, 140]
|
451
|
+
NAMES['teal'] = [0, 128, 128]
|
452
|
+
NAMES['thistle'] = [216, 191, 216]
|
453
|
+
NAMES['tomato'] = [255, 99, 71]
|
454
|
+
NAMES['turquoise'] = [64, 224, 208]
|
455
|
+
NAMES['violet'] = [238, 130, 238]
|
456
|
+
NAMES['wheat'] = [245, 222, 179]
|
457
|
+
NAMES['white'] = [255, 255, 255]
|
458
|
+
NAMES['whitesmoke'] = [245, 245, 245]
|
459
|
+
NAMES['yellow'] = [255, 255, 0]
|
460
|
+
NAMES['yellowgreen'] = [154, 205, 50]
|
461
|
+
|
462
|
+
#
|
463
|
+
# named colors
|
464
|
+
#---------------------------------------------------------------------------
|
465
|
+
|
466
|
+
|
467
|
+
#---------------------------------------------------------------------------
|
468
|
+
# method_missing
|
469
|
+
#
|
470
|
+
|
471
|
+
# This method allows you to create colors by name as if the color names were
|
472
|
+
# methods. For example, RGBCell.red returns an object representing the color
|
473
|
+
# red.
|
474
|
+
def self.method_missing(key)
|
475
|
+
# $tm.hrm
|
476
|
+
# puts key.class
|
477
|
+
# puts NAMES[key.to_s]
|
478
|
+
# $tm.devexit
|
479
|
+
|
480
|
+
if name = NAMES[key.to_s]
|
481
|
+
return self.new(name)
|
482
|
+
else
|
483
|
+
super(key)
|
484
|
+
end
|
485
|
+
end
|
486
|
+
|
487
|
+
#
|
488
|
+
# method_missing
|
489
|
+
#---------------------------------------------------------------------------
|
490
|
+
|
491
|
+
|
492
|
+
#---------------------------------------------------------------------------
|
493
|
+
# froms
|
494
|
+
#
|
495
|
+
|
496
|
+
# Returns the distance from the color black.
|
497
|
+
def from_black
|
498
|
+
return distance(self.class.black)
|
499
|
+
end
|
500
|
+
|
501
|
+
# Returns the distance from the color white.
|
502
|
+
def from_white
|
503
|
+
return distance(self.class.white)
|
504
|
+
end
|
505
|
+
|
506
|
+
#
|
507
|
+
# froms
|
508
|
+
#---------------------------------------------------------------------------
|
509
|
+
|
510
|
+
|
511
|
+
#---------------------------------------------------------------------------
|
512
|
+
# comparison operators
|
513
|
+
#
|
514
|
+
# def >(other)
|
515
|
+
# return (from_white() > other.from_white)
|
516
|
+
# end
|
517
|
+
#
|
518
|
+
# def ==(other)
|
519
|
+
# return (from_white.to_s == other.from_white)
|
520
|
+
# end
|
521
|
+
#
|
522
|
+
# def <(other)
|
523
|
+
# return (from_white() < other.from_white)
|
524
|
+
# end
|
525
|
+
#
|
526
|
+
# def <=>(other)
|
527
|
+
# return (from_white() <=> other.from_white())
|
528
|
+
# end
|
529
|
+
#
|
530
|
+
# comparison operators
|
531
|
+
#---------------------------------------------------------------------------
|
532
|
+
|
533
|
+
|
534
|
+
#---------------------------------------------------------------------------
|
535
|
+
# pivot
|
536
|
+
#
|
537
|
+
|
538
|
+
# Returns a new RGBCell object in which the component colors have been
|
539
|
+
# pivoted. Red becomes blue, green becomes red, blue becomes green.
|
540
|
+
def pivot
|
541
|
+
return self.class.new(@green, @blue, @red)
|
542
|
+
end
|
543
|
+
|
544
|
+
# Pivots the objects colors in place.
|
545
|
+
def pivot!
|
546
|
+
rgb = [@green, @blue, @red]
|
547
|
+
@red = rgb[0]
|
548
|
+
@green = rgb[1]
|
549
|
+
@blue = rgb[2]
|
550
|
+
return self
|
551
|
+
end
|
552
|
+
|
553
|
+
#
|
554
|
+
# pivot
|
555
|
+
#---------------------------------------------------------------------------
|
556
|
+
|
557
|
+
|
558
|
+
# private
|
559
|
+
private
|
560
|
+
|
561
|
+
|
562
|
+
#---------------------------------------------------------------------------
|
563
|
+
# rand_255
|
564
|
+
#
|
565
|
+
def self.rand_255
|
566
|
+
return rand(0..255).to_f
|
567
|
+
end
|
568
|
+
#
|
569
|
+
# rand_255
|
570
|
+
#---------------------------------------------------------------------------
|
571
|
+
end
|
572
|
+
#
|
573
|
+
# RGBCell
|
574
|
+
#===============================================================================
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rgbcell
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.5'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mike O'Sullivan
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-06-25 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Calculates colors based on their x-y-z coordinates. Used for cellular
|
14
|
+
automata.
|
15
|
+
email: mike@idocs.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- README.md
|
21
|
+
- lib/rgbcell.rb
|
22
|
+
homepage: https://rubygems.org/gems/rgbcell
|
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: Color calculations using Cartesian coordinat geometery
|
46
|
+
test_files: []
|