Class: RGBCell

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

Overview

An RGBCell object represents a single color in the red-green-blue color scheme, such as is used in HTML or CSS.

Constant Summary collapse

VERSION =

version

'0.5'
NAMES =

A hash of official named HTML colors. For the full list of names, see www.w3.org/wiki/CSS/Properties/color/keywords

{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*opts) ⇒ RGBCell

Initializes a new RGBCell object. The params can be in one of several forms.

  • three numbers

    RGBCell.new(127, 255, 0)
    
  • a named color

    RGBCell.new('orange')
    
  • a hexidecimal expression

    RGBCell.new('#7fff00')
    
  • a random color

    RGBCell.new('random')
    
  • no param returns black

    RGBCell.new
    


34
35
36
37
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rgbcell.rb', line 34

def initialize(*opts)
	# $tm.hrm
	
	# no opts: set to black
	if opts.length == 0
		@red = 0.0
		@green = 0.0
		@blue = 0.0
		
	# 1 opt
	elsif opts.length == 1
		# array
		if opts[0].is_a?(Array)
			@red = opts[0][0].to_f
			@green = opts[0][1].to_f
			@blue = opts[0][2].to_f
		
		# random
		elsif opts[0] == 'random'
			randomize()
			
		# named
		elsif coords = NAMES[opts[0]]
			@red = coords[0].to_f
			@green = coords[1].to_f
			@blue = coords[2].to_f
			
		# hex
		elsif opts[0].match(/\A\#?[a-f0-9]{6,}\z/mu)
			rgbs = opts[0]
			rgbs = rgbs.sub(/\A\#/mu, '')
			@red = rgbs[0..1].hex
			@green = rgbs[2..3].hex
			@blue = rgbs[4..5].hex
		
		# else unknown opt
		else
			raise 'unknown-opt: ' + opts[0].to_s
		end
		
	# 3 opts
	elsif opts.length == 3
		@red = opts[0].to_f
		@green = opts[1].to_f
		@blue = opts[2].to_f
		
	# any other number of options is invalid
	else
		raise 'invalid-initialize-options-length: ' + opts.length.to_s
	end
end

Instance Attribute Details

#blueObject

The value of the blue component.



101
102
103
# File 'lib/rgbcell.rb', line 101

def blue
  @blue
end

#greenObject

The value of the green component.



98
99
100
# File 'lib/rgbcell.rb', line 98

def green
  @green
end

#redObject

The value of the red component.



95
96
97
# File 'lib/rgbcell.rb', line 95

def red
  @red
end

Class Method Details

.average(*colors) ⇒ Object

Returns the midpoint of the given colors expressed as a color.



218
219
220
221
# File 'lib/rgbcell.rb', line 218

def self.average(*colors)
	base = self.obj_or_new(colors.shift)
	return base.average(*colors)
end

.coord_to_hex(coord) ⇒ Object

Converts a color coordinate to hexidecimal.



265
266
267
# File 'lib/rgbcell.rb', line 265

def self.coord_to_hex(coord)
	return coord.to_i.to_s(16).rjust(2, '0')
end

.method_missing(key) ⇒ Object

This method allows you to create colors by name as if the color names were methods. For example, RGBCell.red returns an object representing the color red.



474
475
476
477
478
479
480
481
482
483
484
485
# File 'lib/rgbcell.rb', line 474

def self.method_missing(key)
	# $tm.hrm
	# puts key.class
	# puts NAMES[key.to_s]
	# $tm.devexit
	
	if name = NAMES[key.to_s]
		return self.new(name)
	else
		super(key)
	end
end

.obj_or_new(*positions) ⇒ Object

If the given object is an EGBCell object, then it is returned. Otherwise the object is used to create an RGBCell object.



115
116
117
118
119
120
121
# File 'lib/rgbcell.rb', line 115

def self.obj_or_new(*positions)
	if positions[0].is_a?(RGBCell)
		return positions[0]
	else
		return self.new(*positions)
	end
end

Instance Method Details

#-(*other) ⇒ Object

Alias for RGBCell#distance.



303
304
305
# File 'lib/rgbcell.rb', line 303

def -(*other)
	return distance(*other)
end

#average(*others) ⇒ Object

Returns a color representing the midpoint between the object’s color and the other given colors.



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/rgbcell.rb', line 187

def average(*others)
	# $tm.hrm
	r_sum = @red
	g_sum = @green
	b_sum = @blue
	
	# loop through others
	others.each do |other|
		other = self.class.obj_or_new(other)
		r_sum += other.red
		g_sum += other.green
		b_sum += other.blue
	end
	
	# total
	total = others.length + 1
	
	# return
	return self.class.new( r_sum/total, g_sum/total, b_sum/total )
end

#distance(*other) ⇒ Object

Returns the distance between the color and the given color. Always returns zero or a positive number.



279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/rgbcell.rb', line 279

def distance(*other)
	# $tm.hrm
	
	# ensure object
	other = self.class.obj_or_new(*other)
	
	# individual distances
	red_d = (@red - other.red).abs2
	green_d = (@green - other.green).abs2
	blue_d = (@blue - other.blue).abs2
	
	# rv
	return Math.sqrt(red_d + green_d + blue_d).abs
end

#from_blackObject

Returns the distance from the color black.



497
498
499
# File 'lib/rgbcell.rb', line 497

def from_black
	return distance(self.class.black)
end

#from_whiteObject

Returns the distance from the color white.



502
503
504
# File 'lib/rgbcell.rb', line 502

def from_white
	return distance(self.class.white)
end

#pivotObject

Returns a new RGBCell object in which the component colors have been pivoted. Red becomes blue, green becomes red, blue becomes green.



540
541
542
# File 'lib/rgbcell.rb', line 540

def pivot
	return self.class.new(@green, @blue, @red)
end

#pivot!Object

Pivots the objects colors in place.



545
546
547
548
549
550
551
# File 'lib/rgbcell.rb', line 545

def pivot!
	rgb = [@green, @blue, @red]
	@red = rgb[0]
	@green = rgb[1]
	@blue = rgb[2]
	return self
end

#positionsObject

Returns an array of the three component coordinates.



157
158
159
# File 'lib/rgbcell.rb', line 157

def positions()
	return [@red, @green, @blue]
end

#randomizeObject

Sets the object to a random color.



170
171
172
173
174
# File 'lib/rgbcell.rb', line 170

def randomize
	@red = self.class.rand_255().to_f
	@green = self.class.rand_255().to_f
	@blue = self.class.rand_255().to_f
end

#to_sObject

Returns the hexidecimal representation of the color.



233
234
235
# File 'lib/rgbcell.rb', line 233

def to_s
	return to_str()
end

#to_strObject

Returns the hexidecimal representation of the color.



238
239
240
241
242
243
244
245
246
247
248
# File 'lib/rgbcell.rb', line 238

def to_str
	rv = '#'
	
	# loop through coordinates
	positions.each do |pos|
		rv += self.class.coord_to_hex(pos)
	end
	
	# return
	return rv
end