Class: Color::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 = Color::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 color:

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


67
68
69
70
71
# File 'lib/quality_extensions/color/rgb.rb', line 67

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:



58
59
60
# File 'lib/quality_extensions/color/rgb.rb', line 58

def blue
  @blue
end

#greenObject (readonly)

:startdoc:



58
59
60
# File 'lib/quality_extensions/color/rgb.rb', line 58

def green
  @green
end

#redObject (readonly)

:startdoc:



58
59
60
# File 'lib/quality_extensions/color/rgb.rb', line 58

def red
  @red
end

Instance Method Details

#==(other) ⇒ Object



93
94
95
# File 'lib/quality_extensions/color/rgb.rb', line 93

def ==(other)
  to_s == other.to_s
end

#inspectObject



81
82
83
# File 'lib/quality_extensions/color/rgb.rb', line 81

def inspect
  "<Color::RGB '#{to_s}'>"
end

#to_iObject

Returns the integral representation of the color, f.e.:

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


89
90
91
# File 'lib/quality_extensions/color/rgb.rb', line 89

def to_i
  red*65536 + green*256 + blue
end

#to_sObject

Returns the hexadecimal string representation of the color, f.e.:

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


77
78
79
# File 'lib/quality_extensions/color/rgb.rb', line 77

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