Class: Rubygame::Color::ColorHSV
- Inherits:
-
Object
- Object
- Rubygame::Color::ColorHSV
- Includes:
- ColorBase
- Defined in:
- lib/rubygame/color/models/hsv.rb
Overview
Represents color in the HSV (Hue, Saturation, Value) color space.
Instance Attribute Summary collapse
-
#a ⇒ Object
readonly
Returns the value of attribute a.
-
#h ⇒ Object
readonly
Returns the value of attribute h.
-
#s ⇒ Object
readonly
Returns the value of attribute s.
-
#v ⇒ Object
readonly
Returns the value of attribute v.
Class Method Summary collapse
-
.hsva_to_rgba(h, s, v, a) ⇒ Object
Convert the hue, saturation, value, and alpha to the equivalent red, green, blue, and alpha.
- .new_from_rgba(rgba) ⇒ Object
- .new_from_sdl_rgba(rgba) ⇒ Object
-
.rgba_to_hsva(r, g, b, a) ⇒ Object
Convert the red, green, blue, and alpha to the equivalent hue, saturation, value, and alpha.
Instance Method Summary collapse
-
#initialize(color) ⇒ ColorHSV
constructor
call-seq: new( [h,s,v,a] ) -> ColorHSV new( [h,s,v] ) -> ColorHSV new( color ) -> ColorHSV.
-
#to_rgba_ary ⇒ Object
Return an Array with the red, green, blue, and alpha components of the color (converting the color to the RGBA model first).
- #to_s ⇒ Object (also: #inspect)
Methods included from ColorBase
#*, #+, #-, #/, #average, #over
Constructor Details
#initialize(color) ⇒ ColorHSV
call-seq:
new( [h,s,v,a] ) -> ColorHSV
new( [h,s,v] ) -> ColorHSV
new( color ) -> ColorHSV
Create a new instance from an Array or an existing color (of any type). If the alpha (opacity) component is omitted from the array, full opacity will be used.
All color components range from 0.0 to 1.0.
42 43 44 45 46 47 48 49 |
# File 'lib/rubygame/color/models/hsv.rb', line 42 def initialize( color ) if color.kind_of?(Array) @h, @s, @v, @a = color.collect { |i| i.to_f } @a = 1.0 unless @a elsif color.respond_to?(:to_rgba_ary) @h, @s, @v, @a = self.class.rgba_to_hsva( *color.to_rgba_ary ) end end |
Instance Attribute Details
#a ⇒ Object (readonly)
Returns the value of attribute a.
29 30 31 |
# File 'lib/rubygame/color/models/hsv.rb', line 29 def a @a end |
#h ⇒ Object (readonly)
Returns the value of attribute h.
29 30 31 |
# File 'lib/rubygame/color/models/hsv.rb', line 29 def h @h end |
#s ⇒ Object (readonly)
Returns the value of attribute s.
29 30 31 |
# File 'lib/rubygame/color/models/hsv.rb', line 29 def s @s end |
#v ⇒ Object (readonly)
Returns the value of attribute v.
29 30 31 |
# File 'lib/rubygame/color/models/hsv.rb', line 29 def v @v end |
Class Method Details
.hsva_to_rgba(h, s, v, a) ⇒ Object
Convert the hue, saturation, value, and alpha to the equivalent red, green, blue, and alpha.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/rubygame/color/models/hsv.rb', line 110 def hsva_to_rgba( h, s, v, a ) # :nodoc: # Determine what part of the "color hexagon" the hue is in. hi = (h * 6).floor % 6 # Fractional part f = (h * 6) - hi # Helper values p = v * (1.0 - s) q = v * (1.0 - (f * s)) t = v * (1.0 - ((1.0 - f) * s)) # Finally calculate the rgb values r, g, b = calculate_rgb(hi, v, p, t, q) return [r, g, b, a] end |
.new_from_rgba(rgba) ⇒ Object
64 65 66 |
# File 'lib/rubygame/color/models/hsv.rb', line 64 def new_from_rgba( rgba ) new( rgba_to_hsva(*rgba) ) end |
.new_from_sdl_rgba(rgba) ⇒ Object
68 69 70 |
# File 'lib/rubygame/color/models/hsv.rb', line 68 def new_from_sdl_rgba( rgba ) new_from_rgba( rgba.collect { |i| i / 255.0 } ) end |
.rgba_to_hsva(r, g, b, a) ⇒ Object
Convert the red, green, blue, and alpha to the equivalent hue, saturation, value, and alpha.
74 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 |
# File 'lib/rubygame/color/models/hsv.rb', line 74 def rgba_to_hsva( r, g, b, a ) # :nodoc: rgb_arr = [r, g, b] max = rgb_arr.max min = rgb_arr.min # Calculate hue. if min == max h = 0 # Undefined in this case, but set it to zero elsif max == r and g >= b h = (1.quo(6) * (g - b) / (max - min)) + 0 elsif max == r and g < b h = (1.quo(6) * (g - b) / (max - min)) + 1.0 elsif max == g h = (1.quo(6) * (b - r) / (max - min)) + 1.quo(3) elsif max == b h = (1.quo(6) * (r - g) / (max - min)) + 2.quo(3) else raise "Should never happen" end # Calulate value. v = max # Calculate saturation. if max == 0.0 s = 0.0 else s = 1.0 - (min / max) end return [h,s,v,a] end |
Instance Method Details
#to_rgba_ary ⇒ Object
Return an Array with the red, green, blue, and alpha components of the color (converting the color to the RGBA model first).
53 54 55 |
# File 'lib/rubygame/color/models/hsv.rb', line 53 def to_rgba_ary return self.class.hsva_to_rgba( @h, @s, @v, @a ) end |
#to_s ⇒ Object Also known as: inspect
57 58 59 |
# File 'lib/rubygame/color/models/hsv.rb', line 57 def to_s "#<#{self.class} [#{@h}, #{@s}, #{@v}, #{@a}]>" end |