RGBCell
RGBCell represents a color as the coordinates in the x-y-z coordinate system. Each of the three component color elements, red, green, and blue, have a float value from 0 to 256.
Creating an object without any params creates black:
1. require 'rgbcell'
2. color = RGBCell.new
3. color.to_s #=> #000000
4. color.red #=> 0.0
5. color.green #=> 0.0
6. color.blue #=> 0.0
Line 1 loads the RGBCell module. Line 2 create an RGBCell object. Because there are no parameters given, the object represents black. Line 3 show the stringification of the color such as might be used in CSS. Lines 4-6 show the numeric values of the three component colors, which are stored as floats.
There are several ways to indicate the color in RGBCell::new. One way is to give its three components as numeric values.
color = RGBCell.new(127, 255, 0)
color.to_s #=> #7fff00
You can indicate the color using hex format:
color = RGBCell.new('#7fff00')
color.to_s #=> #7fff00
color.red #=> 127.0
color.green #=> 255.0
color.blue #=> 0.0
You can give one of the HTML named colors:
color = RGBCell.new('purple')
color.to_s #=> #800080
color.red #=> 128.0
color.green #=> 0.0
color.blue #=> 128.0
Finally, you can use 'random' to get a random color:
color = RGBCell.new('random')
color.to_s #=> #dbd25b
color.red #=> 219.0
color.green #=> 210.0
color.blue #=> 91.0
You can also set each component color individually. Notice in this example that a component integer value is converted to float.
color = RGBCell.new()
color.red = 60 #=> 60.0
color.green = 230 #=> 210.0
color.blue = 91.005 #=> 91.0005
color.to_s #=> #3ce65b
Distance
Because every color is a set of coordinates, the difference between two colors can be expressed as a distance. The distance is always zero or a positive number. For example, the following example gives the distance between red and chartreuse.
red = RGBCell.new('red')
chartreuse = RGBCell.new('chartreuse')
red.distance(chartreuse) #=> 285.3226244096321
The same thing can be expressed with the minus operator.
puts red - chartreuse #=> 285.3226244096321
The second color does not have to be an RGBCell object; it only needs to be an expression that can be used to create an RGBCell object.
puts red - 'green' #=> 360.62445840513925
Average
Two or more colors can be averaged by calculating the midpoint of their
coordinates using the average
method. The result of the average is itself an
RGBCell object.
red = RGBCell.new('red')
tomato = RGBCell.new('tomato')
avg = red.average(tomato)
puts avg.class #=> RGBCell
puts avg.to_s #=> #ff3100
You can pass in more than one color for averaging. You can use RGBCell objects or any object that can be used to create an RGBCell object.
tomato = RGBCell.new('tomato')
puts tomato.average('orange', 'olive') #=> #d48200
If you prefer not to create any colors at all, you can use the class method.
puts RGBCell.average('tomato', 'orange', 'olive') #=> #d48200
Install
gem install rgbcube
Author
Mike O'Sullivan [email protected]
History
version | date | notes |
---|---|---|
0.5 | June 25, 2020 | Initial upload. |