Class: Color::RGB

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/color/rgb.rb

Constant Summary collapse

RGB_VALUES =
'([0-9a-f]{2})'
RRGGBB_REGEXP =

not supported, although in CSS: RGB_REGEXP = Regexp.new ‘(’ + RGB_VALUES + ‘)3’, Regexp::IGNORECASE

Regexp.new '^\#?' + (RGB_VALUES * 3) + '$', Regexp::IGNORECASE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(red, green, blue) ⇒ RGB

Returns a new instance of RGB.



48
49
50
51
52
53
54
55
56
# File 'lib/color/rgb.rb', line 48

def initialize(red, green, blue)
  [ [ red, "red" ], [ green, "green" ], [ blue, "blue" ] ].each do |val, str|
    Color::check_constraint(val, str, 0, 255)
  end

  @red   = red.to_i
  @green = green.to_i
  @blue  = blue.to_i
end

Instance Attribute Details

#blueObject

Returns the value of attribute blue.



14
15
16
# File 'lib/color/rgb.rb', line 14

def blue
  @blue
end

#greenObject

Returns the value of attribute green.



14
15
16
# File 'lib/color/rgb.rb', line 14

def green
  @green
end

#redObject

Returns the value of attribute red.



14
15
16
# File 'lib/color/rgb.rb', line 14

def red
  @red
end

Class Method Details

.matches_rgb_string?(val) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/color/rgb.rb', line 43

def matches_rgb_string?(val)
  val.kind_of?(String) && RRGGBB_REGEXP.match(val)
end

.new(*args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/color/rgb.rb', line 22

def new(*args)
  if args
    if args.size == 3
      new_orig(*args)
    elsif args.size == 1 and args[0].kind_of? String
      str = args[0].sub(%r{^\#}, '') # optional
      # str.hex does not validate, so we do here:
      if md = matches_rgb_string?(str)
        r, g, b = (1 .. 3).collect { |idx| md[idx].hex }
        new_orig r, g, b
      else
        raise RuntimeError.new("invalid RGB string '#{str}: does not match #{RRGGBB_REGEXP.source}")
      end
    else
      raise RuntimeError.new("invalid arguments '#{args.inspect}': expecting 0, 1, or 3")
    end
  else
    new_orig 0, 0, 0
  end
end

.new_origObject



21
# File 'lib/color/rgb.rb', line 21

alias_method :new_orig, :new

Instance Method Details

#<=>(other) ⇒ Object



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

def <=>(other)
  to_i <=> other.to_i
end

#==(other) ⇒ Object



70
71
72
# File 'lib/color/rgb.rb', line 70

def ==(other)
  to_i == other.to_i
end

#to_hsbObject

Converts RGB to an HSB object.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/color/rgb.rb', line 75

def to_hsb
  # this is not the only implementation of RGB to HSB, but it works.

  # scale from 0..255 to 0..1
  r, g, b = [ red, green, blue ].collect { |v| v / 255.0 }

  log "r", r
  log "g", g
  log "b", b
  
  minrgb = [ r, g, b ].min
  log "minrgb", minrgb
  maxrgb = [ r, g, b ].max
  log "maxrgb", maxrgb
  delta  = maxrgb - minrgb
  log "delta", delta
  
  hue = nil
  saturation = nil
  brightness = maxrgb
  
  if delta == 0
    # this is a gray, no chroma...
    hue = 0
    saturation = 0
  else                        # chromatic data...
    saturation = delta / maxrgb

    del_red, del_green, del_blue = [ r, g, b ].collect do |v|
      (((maxrgb - v) / 6.0) + (delta / 2.0)) / delta
    end
    
    if r == maxrgb
      hue = del_blue - del_green
    elsif g == maxrgb
      hue = (1.0 / 3) + del_red - del_blue
    elsif b == maxrgb
      hue = (2.0 / 3) + del_green - del_red
    end
    
    if hue < 0
      hue += 1
    end
    
    if hue > 1
      hue -= 1
    end
  end

  log "hue", hue
  log "saturation", saturation
  log "brightness", brightness
  
  # hue is to one decimal place; saturation and brightness to three
  Color::HSB.new Color::round(hue * 360, 1), Color::round(saturation, 3), Color::round(brightness, 3)
end

#to_iObject



62
63
64
# File 'lib/color/rgb.rb', line 62

def to_i
  (red << 16) | (green << 8) | blue
end

#to_sObject



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

def to_s
  "#{red},#{green},#{blue}"
end