Class: Rubygame::Color::ColorHSL

Inherits:
Object
  • Object
show all
Includes:
ColorBase
Defined in:
lib/rubygame/color/models/hsl.rb

Overview

Represents color in the HSL (Hue, Saturation, Luminosity) color space.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ColorBase

#*, #+, #-, #/, #average, #over

Constructor Details

#initialize(color) ⇒ ColorHSL

call-seq:

new( [h,s,l,a] )  ->  ColorHSL
new( [h,s,l] )  ->  ColorHSL
new( color )  ->  ColorHSL

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/hsl.rb', line 42

def initialize( color )
	if color.kind_of?(Array)
		@h, @s, @l, @a = color.collect { |i| i.to_f }
		@a = 1.0 unless @a
	elsif color.respond_to?(:to_rgba_ary)
		@h, @s, @l, @a = self.class.rgba_to_hsla( *color.to_rgba_ary )
	end
end

Instance Attribute Details

#aObject (readonly)

Returns the value of attribute a.



29
30
31
# File 'lib/rubygame/color/models/hsl.rb', line 29

def a
  @a
end

#hObject (readonly)

Returns the value of attribute h.



29
30
31
# File 'lib/rubygame/color/models/hsl.rb', line 29

def h
  @h
end

#lObject (readonly)

Returns the value of attribute l.



29
30
31
# File 'lib/rubygame/color/models/hsl.rb', line 29

def l
  @l
end

#sObject (readonly)

Returns the value of attribute s.



29
30
31
# File 'lib/rubygame/color/models/hsl.rb', line 29

def s
  @s
end

Class Method Details

.hsla_to_rgba(h, s, l, a) ⇒ Object

Convert the hue, saturation, luminosity, and alpha to the equivalent red, green, blue, and alpha.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rubygame/color/models/hsl.rb', line 112

def hsla_to_rgba( h, s, l, a ) # :nodoc:
	# If the color is achromatic, return already with the lightness value for all components
	if s == 0.0
		return [l, l, l, a]
	end

	# Otherwise, we have to do the long, hard calculation

	# q helper value
	q = (l < 0.5) ? (l * (1.0 + s)) : (l + s - l * s)

	# p helper value
	p = (2.0 * l) - q

	r = calculate_component( p, q, h + 1.quo(3) )
	g = calculate_component( p, q, h            )
	b = calculate_component( p, q, h - 1.quo(3) )

	return [r,g,b,a]
end

.new_from_rgba(rgba) ⇒ Object



64
65
66
# File 'lib/rubygame/color/models/hsl.rb', line 64

def new_from_rgba( rgba )
	new( rgba_to_hsla(*rgba) )
end

.new_from_sdl_rgba(rgba) ⇒ Object



68
69
70
# File 'lib/rubygame/color/models/hsl.rb', line 68

def new_from_sdl_rgba( rgba )
	new_from_rgba( rgba.collect { |i| i / 255.0 } )
end

.rgba_to_hsla(r, g, b, a) ⇒ Object

Convert the red, green, blue, and alpha to the equivalent hue, saturation, luminosity, 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
107
108
# File 'lib/rubygame/color/models/hsl.rb', line 74

def rgba_to_hsla( r, g, b, a ) # :nodoc:
	rgb_arr = [r, g, b]
	max     = rgb_arr.max
	min     = rgb_arr.min

	# Calculate lightness.
	l = (max + min) / 2.0

	# Calculate saturation.
	if l == 0.0 or max == min
		s = 0
	elsif 0 < l and l <= 0.5 
		s = (max - min) / (max + min)
	else # l > 0.5
		s = (max - min) / (2 - (max + min))
	end
	
	# 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 
	
	return [h,s,l,a]
end

Instance Method Details

#to_rgba_aryObject

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/hsl.rb', line 53

def to_rgba_ary
	return self.class.hsla_to_rgba( @h, @s, @l, @a )
end

#to_sObject Also known as: inspect



57
58
59
# File 'lib/rubygame/color/models/hsl.rb', line 57

def to_s
	"#<#{self.class} [#{@h}, #{@s}, #{@l}, #{@a}]>"
end