Class: TuyaCloud::Device::ColorLight::ColorSetting

Inherits:
Object
  • Object
show all
Defined in:
lib/tuya_cloud/device.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json) ⇒ ColorSetting

Returns a new instance of ColorSetting.



132
133
134
135
136
137
138
139
# File 'lib/tuya_cloud/device.rb', line 132

def initialize(json)
  return unless json

  self.saturation = json['saturation']
  self.brightness = json['brightness']
  self.hue        = json['hue']
  self.rgb        = from_hsb(hue, saturation, brightness)
end

Instance Attribute Details

#brightnessObject

Returns the value of attribute brightness.



127
128
129
# File 'lib/tuya_cloud/device.rb', line 127

def brightness
  @brightness
end

#hueObject

Returns the value of attribute hue.



127
128
129
# File 'lib/tuya_cloud/device.rb', line 127

def hue
  @hue
end

#rgbObject

Returns the value of attribute rgb.



127
128
129
# File 'lib/tuya_cloud/device.rb', line 127

def rgb
  @rgb
end

#saturationObject

Returns the value of attribute saturation.



127
128
129
# File 'lib/tuya_cloud/device.rb', line 127

def saturation
  @saturation
end

Instance Method Details

#from_hsb(h, s, v) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/tuya_cloud/device.rb', line 142

def from_hsb(h, s, v)
  h, s, v = h.to_f / 360, s.to_f / 100, v.to_f / 100
  h_i = (h * 6).to_i
  f = h * 6 - h_i
  p = v * (1 - s)
  q = v * (1 - f * s)
  t = v * (1 - (1 - f) * s)
  r, g, b = v, t, p if h_i == 0
  r, g, b = q, v, p if h_i == 1
  r, g, b = p, v, t if h_i == 2
  r, g, b = p, q, v if h_i == 3
  r, g, b = t, p, v if h_i == 4
  r, g, b = v, p, q if h_i == 5
  self.rgb = '#'\
             "#{(r * 255).round.to_s(16).rjust(2, '0')}"\
             "#{(g * 255).round.to_s(16).rjust(2, '0')}"\
             "#{(b * 255).round.to_s(16).rjust(2, '0')}"
end

#from_rgb(r, g, b) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/tuya_cloud/device.rb', line 161

def from_rgb(r, g, b)
  self.rgb = '#'\
             "#{r.to_s(16).rjust(2, '0')}"\
             "#{g.to_s(16).rjust(2, '0')}"\
             "#{b.to_s(16).rjust(2, '0')}"
  r /= 255.0
  g /= 255.0
  b /= 255.0
  max = [r, g, b].max
  min = [r, g, b].min
  delta = max - min
  v = max * 255
  s = 0.0
  s = delta / max * 255 if max != 0.0
  if s == 0.0
    h = 0.0
  else
    if r == max
      h = (g - b) / delta
    elsif g == max
      h = 2 + (b - r) / delta
    elsif b == max
      h = 4 + (r - g) / delta
    end
    h *= 60.0
    h += 360.0 if h.negative?
  end
  self.hue = h.round
  self.saturation = s.round
  self.brightness = v.round
end

#to_hObject



193
194
195
196
197
198
# File 'lib/tuya_cloud/device.rb', line 193

def to_h
  { hue: hue,
    saturation: saturation,
    brightness: brightness,
    rgb: rgb }
end