Class: Colorable::RGB
- Inherits:
-
ColorSpace
- Object
- ColorSpace
- Colorable::RGB
- Defined in:
- lib/colorable/color_space.rb
Instance Attribute Summary collapse
-
#b ⇒ Object
(also: #blue)
Returns the value of attribute b.
-
#g ⇒ Object
(also: #green)
Returns the value of attribute g.
-
#r ⇒ Object
(also: #red)
Returns the value of attribute r.
-
#rgb ⇒ Object
(also: #to_a)
Returns the value of attribute rgb.
Instance Method Summary collapse
-
#*(other) ⇒ Object
Color multiplication.
-
#+(other) ⇒ Object
Color addition.
-
#-(other) ⇒ Object
Color subtruction.
-
#/(other) ⇒ Object
Color division.
-
#initialize(r = 0, g = 0, b = 0) ⇒ RGB
constructor
A new instance of RGB.
- #to_hex ⇒ Object
- #to_hsb ⇒ Object
- #to_name ⇒ Object
Methods inherited from ColorSpace
#<=>, #coerce, #move_to_top, #to_s
Methods included from Converter
#hex2rgb, #hsb2rgb, #name2rgb, #rgb2hex, #rgb2hsb, #rgb2hsl, #rgb2name
Constructor Details
#initialize(r = 0, g = 0, b = 0) ⇒ RGB
Returns a new instance of RGB.
45 46 47 |
# File 'lib/colorable/color_space.rb', line 45 def initialize(r=0,g=0,b=0) @r, @g, @b = @rgb = validate_rgb([r, g, b]) end |
Instance Attribute Details
#b ⇒ Object Also known as: blue
Returns the value of attribute b.
44 45 46 |
# File 'lib/colorable/color_space.rb', line 44 def b @b end |
#g ⇒ Object Also known as: green
Returns the value of attribute g.
44 45 46 |
# File 'lib/colorable/color_space.rb', line 44 def g @g end |
#r ⇒ Object Also known as: red
Returns the value of attribute r.
44 45 46 |
# File 'lib/colorable/color_space.rb', line 44 def r @r end |
#rgb ⇒ Object Also known as: to_a
Returns the value of attribute rgb.
44 45 46 |
# File 'lib/colorable/color_space.rb', line 44 def rgb @rgb end |
Instance Method Details
#*(other) ⇒ Object
Color multiplication
other
should be a Color object. It applies multiply compositing.
95 96 97 98 99 |
# File 'lib/colorable/color_space.rb', line 95 def *(other) raise ArgumentError, "Invalid argument given" unless other.is_a?(RGB) rgb = compound_by(other.rgb) { |a, b| [(a*b/255.0).round, 0].max } self.class.new *rgb end |
#+(other) ⇒ Object
Color addition
other
can be:
RGB object: apply minimum compositing.
Array of RGB values: each values added to each of RGBs.
Fixnum: other number added to all of RGBs.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/colorable/color_space.rb', line 59 def +(other) rgb = case other when RGB compound_by(other.rgb) { |a, b| [a+b, 255].min } when Array raise ArgumentError, "Invalid size of Array given" unless other.size==3 compound_by(other) { |a, b| (a + b) % 256 } when Fixnum compound_by([other] * 3) { |a, b| (a + b) % 256 } else raise ArgumentError, "Invalid argument given" end self.class.new *rgb end |
#-(other) ⇒ Object
Color subtruction
other
can be:
RGB object: apply maximum compositing.
Array of RGB values: each values added to each of RGBs.
Fixnum: other number added to all of RGBs.
81 82 83 84 85 86 87 88 89 |
# File 'lib/colorable/color_space.rb', line 81 def -(other) case other when RGB rgb = compound_by(other.rgb) { |a, b| [a+b-255, 0].max } self.class.new *rgb else super end end |
#/(other) ⇒ Object
Color division
other
should be a Color object. It applies screen compositing.
105 106 107 108 109 |
# File 'lib/colorable/color_space.rb', line 105 def /(other) raise ArgumentError, "Invalid argument given" unless other.is_a?(RGB) rgb = compound_by(other.rgb) { |a, b| [a+b-(a*b/255.0).round, 255].min } self.class.new *rgb end |
#to_hex ⇒ Object
119 120 121 |
# File 'lib/colorable/color_space.rb', line 119 def to_hex rgb2hex(self.to_a) end |
#to_hsb ⇒ Object
115 116 117 |
# File 'lib/colorable/color_space.rb', line 115 def to_hsb rgb2hsb(self.to_a) end |
#to_name ⇒ Object
111 112 113 |
# File 'lib/colorable/color_space.rb', line 111 def to_name rgb2name(self.to_a) end |