class TuyaCloud::Device::ColorLight::ColorSetting

Attributes

brightness[RW]
hue[RW]
rgb[RW]
saturation[RW]

Public Class Methods

new(json) click to toggle source
# 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

Public Instance Methods

from_hsb(h, s, v) click to toggle source

conversions from: gist.github.com/makevoid/3918299

# 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) click to toggle source
# 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_h() click to toggle source
# File lib/tuya_cloud/device.rb, line 193
def to_h
  { hue: hue,
    saturation: saturation,
    brightness: brightness,
    rgb: rgb }
end