Class: Colour::RGB

Inherits:
Object show all
Defined in:
lib/quality_extensions/color/rgb.rb

Overview

A lightweight implementation of rgb/hex colors, designed for web use.

c = Colour::RGB.new(0xFFFFFF)

c.to_s -> "ffffff"

c.red = 196
c.green = 0xDD
c.blue  = 'EE'

c.to_s -> "c4ddee"

Similar to (see also) ColorTools.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*rgb) ⇒ RGB

The following are the same colour:

RGB.new(0xFFFFFF)
RGB.new(:FFFFFF)
RGB.new("FFFFFF")
RGB.new(255, "FF", 0xFF)


55
56
57
58
59
# File 'lib/quality_extensions/color/rgb.rb', line 55

def initialize(*rgb)
  (rgb.size == 1 ? rgb[0].to_rgb : rgb).zip([:red, :green, :blue]) do |(value, col)|
    set!(col, value)
  end
end

Instance Attribute Details

#blueObject (readonly)

:startdoc:



46
47
48
# File 'lib/quality_extensions/color/rgb.rb', line 46

def blue
  @blue
end

#greenObject (readonly)

:startdoc:



46
47
48
# File 'lib/quality_extensions/color/rgb.rb', line 46

def green
  @green
end

#redObject (readonly)

:startdoc:



46
47
48
# File 'lib/quality_extensions/color/rgb.rb', line 46

def red
  @red
end

Instance Method Details

#to_sObject

Returns the hexadecimal string representation of the colour e.g.

RGB.new(255, 255, 255).to_s  -> "FFFFFF"


65
66
67
# File 'lib/quality_extensions/color/rgb.rb', line 65

def to_s
  "%02x%02x%02x" % [ red, green, blue ]
end