class RGBCell

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

Constants

NAMES

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

VERSION

version

Attributes

blue[R]

The value of the blue component.

green[R]

The value of the green component.

red[R]

The value of the red component.

Public Class Methods

average(*colors) click to toggle source

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

# 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) click to toggle source

Converts a color coordinate to hexidecimal.

# 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) click to toggle source

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.

Calls superclass method
# 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
new(*opts) click to toggle source

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
    
# 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
obj_or_new(*positions) click to toggle source

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

# 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

Private Class Methods

rand_255() click to toggle source
# File lib/rgbcell.rb, line 565
def self.rand_255
        return rand(0..255).to_f
end

Public Instance Methods

-(*other) click to toggle source

Alias for RGBCell#distance.

# File lib/rgbcell.rb, line 303
def -(*other)
        return distance(*other)
end
average(*others) click to toggle source

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

# 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
blue=(coord) click to toggle source

Sets the blue coordinate. Converts the given number to a float.

# File lib/rgbcell.rb, line 143
def blue=(coord)
        @blue = coord.to_f
end
distance(*other) click to toggle source

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

# 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_black() click to toggle source

Returns the distance from the color black.

# File lib/rgbcell.rb, line 497
def from_black
        return distance(self.class.black)
end
from_white() click to toggle source

Returns the distance from the color white.

# File lib/rgbcell.rb, line 502
def from_white
        return distance(self.class.white)
end
green=(coord) click to toggle source

Sets the greens coordinate. Converts the given number to a float.

# File lib/rgbcell.rb, line 138
def green=(coord)
        @green = coord.to_f
end
pivot() click to toggle source

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

# File lib/rgbcell.rb, line 540
def pivot
        return self.class.new(@green, @blue, @red)
end
pivot!() click to toggle source

Pivots the objects colors in place.

# File lib/rgbcell.rb, line 545
def pivot!
        rgb = [@green, @blue, @red]
        @red = rgb[0]
        @green = rgb[1]
        @blue = rgb[2]
        return self
end
positions() click to toggle source

Returns an array of the three component coordinates.

# File lib/rgbcell.rb, line 157
def positions()
        return [@red, @green, @blue]
end
randomize() click to toggle source

Sets the object to a random color.

# 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
red=(coord) click to toggle source

Sets the red coordinate. Converts the given number to a float.

# File lib/rgbcell.rb, line 133
def red=(coord)
        @red = coord.to_f
end
to_s() click to toggle source

Returns the hexidecimal representation of the color.

# File lib/rgbcell.rb, line 233
def to_s
        return to_str()
end
to_str() click to toggle source

Returns the hexidecimal representation of the color.

# 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