Class: Hue::Colors::HueSaturation

Inherits:
Color
  • Object
show all
Defined in:
lib/hue/colors/hue_saturation.rb

Constant Summary collapse

HUE_MIN =
0
HUE_MAX =
65536.0
HUE_DEGREES =
360
HUE_SCALE =
HUE_MAX / HUE_DEGREES
SATURATION_MIN =
0
SATURATION_MAX =
255

Constants inherited from Color

Color::ERROR_METHOD_NOT_IMPLEMENTED

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Color

ranged

Constructor Details

#initialize(hue, saturation) ⇒ HueSaturation

Returns a new instance of HueSaturation.



17
18
19
20
# File 'lib/hue/colors/hue_saturation.rb', line 17

def initialize(hue, saturation)
  self.hue = hue
  self.saturation = saturation
end

Instance Attribute Details

#hueObject

Returns the value of attribute hue.



12
13
14
# File 'lib/hue/colors/hue_saturation.rb', line 12

def hue
  @hue
end

#saturationObject Also known as: sat

Returns the value of attribute saturation.



12
13
14
# File 'lib/hue/colors/hue_saturation.rb', line 12

def saturation
  @saturation
end

Instance Method Details

#hue_in_degreesObject



30
31
32
# File 'lib/hue/colors/hue_saturation.rb', line 30

def hue_in_degrees
  self.hue.to_f / HUE_SCALE
end

#hue_in_unit_intervalObject



34
35
36
# File 'lib/hue/colors/hue_saturation.rb', line 34

def hue_in_unit_interval
  hue_in_degrees / HUE_DEGREES
end

#saturation_in_unit_intervalObject Also known as: sat_in_unit_interval



47
48
49
# File 'lib/hue/colors/hue_saturation.rb', line 47

def saturation_in_unit_interval
  self.saturation / SATURATION_MAX.to_f
end

#to_hashObject



56
57
58
# File 'lib/hue/colors/hue_saturation.rb', line 56

def to_hash
  {hue: hue, sat: saturation}
end

#to_rgb(brightness_in_unit_interval = 1.0) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/hue/colors/hue_saturation.rb', line 60

def to_rgb(brightness_in_unit_interval = 1.0)
  h, s, v = hue_in_unit_interval, saturation_in_unit_interval, brightness_in_unit_interval
  if s == 0 #monochromatic
    red = green = blue = v
  else

    v = 1.0 # We are setting the value to 1. Don't count brightness here
    i = (h * 6).floor
    f = h * 6 - i
    p = v * (1 - s)
    q = v * (1 - f * s)
    t = v * (1 - (1 - f) * s)

    case i % 6
    when 0
      red, green, blue = v, t, p
    when 1
      red, green, blue = q, v, p
    when 2
      red, green, blue = p, v, t
    when 3
      red, green, blue = p, q, v
    when 4
      red, green, blue = t, p, v
    when 5
      red, green, blue = v, p, q
    end
  end

  RGB.new(red * RGB::MAX, green * RGB::MAX, blue * RGB::MAX)
end

#to_sObject



52
53
54
# File 'lib/hue/colors/hue_saturation.rb', line 52

def to_s
  "Hue=#{self.hue}, Saturation=#{self.saturation}"
end