Class: ImageRuby::Color

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

Defined Under Namespace

Classes: ArgumentException, OutOfRangeException

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#aObject Also known as: alpha

alpha channel (between 0 and 255)



74
75
76
# File 'lib/imageruby/color.rb', line 74

def a
  @a
end

#bObject Also known as: blue

blue channel (between 0 and 255)



72
73
74
# File 'lib/imageruby/color.rb', line 72

def b
  @b
end

#gObject Also known as: green

green channel (between 0 and 255)



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

def g
  @g
end

#rObject Also known as: red

red channel (between 0 and 255)



68
69
70
# File 'lib/imageruby/color.rb', line 68

def r
  @r
end

Class Method Details

.add_color_name(color_name, value) ⇒ Object

Add a new named Color Example:

Color.add_color_name("turquoise", "#30d5c8")
Color.turqoise # returns recently added named color


38
39
40
41
42
43
44
45
# File 'lib/imageruby/color.rb', line 38

def self.add_color_name(color_name, value)
  color = Color.coerce(value)
  eval("@#{color_name} = color")
  eval("def self.#{color_name}; @#{color_name}; end")

  @color_hash ||= Hash.new
  @color_hash[color_name] = color
end

.coerce(arg) ⇒ Object

Coerce color to convert Array with rgba values, String with hexadecimal web color code, or a Color to a instance of color

Examples:

Color.coerce([255,0,0]) # returns #<ImageRuby::Color:0x7f48b0051040 @r=255, @a=255, @b=0, @g=0>
Color.coerce("#FF0000") # returns #<ImageRuby::Color:0x7f48b0051040 @r=255, @a=255, @b=0, @g=0>
Color.coerce("#F00" ) # returns #<ImageRuby::Color:0x7f48b0051040 @r=255, @a=255, @b=0, @g=0>
Color.coerce(Color.red) # returns #<ImageRuby::Color:0x7f48b0051040 @r=255, @a=255, @b=0, @g=0>


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/imageruby/color.rb', line 143

def self.coerce(arg)
  if arg.instance_of? Color
    return arg
  elsif arg.instance_of? Array
    Color.from_rgba(arg[0],arg[1],arg[2],arg[3] || 255)
  elsif arg.instance_of? String
    if arg.size == 4
      Color.from_rgb((arg[1..1]*2).to_i(16),(arg[2..2]*2).to_i(16),(arg[3..3]*2).to_i(16) )
    elsif arg.size == 7
      Color.from_rgb((arg[1..2]).to_i(16),(arg[3..4]).to_i(16),(arg[5..6]).to_i(16) )
    else
      raise ArgumentException
    end
  else
    raise ArgumentException
  end
end

.define_colors(hash) ⇒ Object

method for internal use



57
58
59
60
61
# File 'lib/imageruby/color.rb', line 57

def self.define_colors(hash)
  hash.each do |k,v|
    add_color_name(k,v)
  end
end

.from_rgb(r_, g_, b_) ⇒ Object

Create a color from given red, green and blue values (between 0 and 255)

Example:

white = Color.from_rgb(255,255,255)


106
107
108
# File 'lib/imageruby/color.rb', line 106

def self.from_rgb(r_,g_,b_)
  from_rgba(r_,g_,b_,255)
end

.from_rgba(r_, g_, b_, a_) ⇒ Object

Create a color from given red, green, blue and alpha. values (between 0 and 255)

Example:

white = Color.from_rgba(255,255,255,128)


115
116
117
118
119
120
121
122
123
124
# File 'lib/imageruby/color.rb', line 115

def self.from_rgba(r_,g_,b_,a_)

  c = new
  c.r = r_
  c.g = g_
  c.b = b_
  c.a = a_

  c
end

.named_colorsObject

return the list of named colors

Color.named_colors.each do |name, color|
  print "color '#{name}' = #{color.inspect}\n"
end


52
53
54
# File 'lib/imageruby/color.rb', line 52

def self.named_colors
  @color_hash
end

Instance Method Details

#==(c) ⇒ Object

Compares two colors channel by channel (including alpha)



127
128
129
130
131
# File 'lib/imageruby/color.rb', line 127

def ==(c)
  return false unless c.instance_of? Color

  (c.r == @r) and (c.g == @g) and (c.b == @b) and (c.a == @a)
end

#inspectObject



161
162
163
# File 'lib/imageruby/color.rb', line 161

def inspect
  "\##{sprintf("%02x%02x%02x",r,g,b)}#{a==255?"": a.to_s(16)}"
end

#to_sObject



172
173
174
# File 'lib/imageruby/color.rb', line 172

def to_s
  self.b.chr + self.g.chr + self.r.chr
end