Class: ImageRuby::Color
Defined Under Namespace
Classes: ArgumentException, OutOfRangeException
Instance Attribute Summary collapse
-
#a ⇒ Object
(also: #alpha)
alpha channel (between 0 and 255).
-
#b ⇒ Object
(also: #blue)
blue channel (between 0 and 255).
-
#g ⇒ Object
(also: #green)
green channel (between 0 and 255).
-
#r ⇒ Object
(also: #red)
red channel (between 0 and 255).
Class Method Summary collapse
-
.add_color_name(color_name, value) ⇒ Object
Add a new named Color Example:.
-
.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.
-
.define_colors(hash) ⇒ Object
method for internal use.
-
.from_rgb(r_, g_, b_) ⇒ Object
Create a color from given red, green and blue values (between 0 and 255).
-
.from_rgba(r_, g_, b_, a_) ⇒ Object
Create a color from given red, green, blue and alpha.
-
.named_colors ⇒ Object
return the list of named colors.
Instance Method Summary collapse
-
#==(c) ⇒ Object
Compares two colors channel by channel (including alpha).
- #inspect ⇒ Object
- #to_s ⇒ Object
Instance Attribute Details
#a ⇒ Object Also known as: alpha
alpha channel (between 0 and 255)
74 75 76 |
# File 'lib/imageruby/color.rb', line 74 def a @a end |
#b ⇒ Object Also known as: blue
blue channel (between 0 and 255)
72 73 74 |
# File 'lib/imageruby/color.rb', line 72 def b @b end |
#g ⇒ Object Also known as: green
green channel (between 0 and 255)
70 71 72 |
# File 'lib/imageruby/color.rb', line 70 def g @g end |
#r ⇒ Object 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
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 |
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 |
#inspect ⇒ Object
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_s ⇒ Object
172 173 174 |
# File 'lib/imageruby/color.rb', line 172 def to_s self.b.chr + self.g.chr + self.r.chr end |