class YeelightCli::ColorProcessor

The class contains color processing methods

Constants

BaseError
InvalidArgumentError

Public Class Methods

color_temperature_to_rgb(color_temperature) click to toggle source
# File lib/yeelight_cli/color_processor.rb, line 10
def color_temperature_to_rgb(color_temperature)
  raise InvalidArgumentError if color_temperature < 600

  green = calculate_rgb_green_by_color_temperature(color_temperature)
  blue = calculate_rgb_blue_by_color_temperature(color_temperature)

  hex_green = green.round.clamp(0, 0xff).to_s(16).rjust(2, '0')
  hex_blue = blue.round.clamp(0, 0xff).to_s(16).rjust(2, '0')

  "ff#{hex_green}#{hex_blue}".to_i(16)
end
huesat_to_rgb(hue, sat) click to toggle source
# File lib/yeelight_cli/color_processor.rb, line 22
def huesat_to_rgb(hue, sat)
  check_huesat!(hue, sat)

  hi = (hue / 60).round % 6
  vmin, vinc, vdec = calculate_huesat_coeffs(hue, sat)

  huesat_coeffs_to_rgb_array(vmin, vinc, vdec, hi)
    .map { |color| (color * 255 / 100).round.to_s(16).rjust(2, '0') }
    .join
    .to_i(16)
end

Private Class Methods

calculate_huesat_coeffs(hue, sat) click to toggle source
# File lib/yeelight_cli/color_processor.rb, line 50
def calculate_huesat_coeffs(hue, sat)
  vmin = 100 - sat
  a = (100 - vmin) * (hue % 60) / 60
  vinc = vmin + a
  vdec = 100 - a
  [vmin, vinc, vdec]
end
calculate_rgb_blue_by_color_temperature(color_temperature) click to toggle source
# File lib/yeelight_cli/color_processor.rb, line 45
def calculate_rgb_blue_by_color_temperature(color_temperature)
  coeff = color_temperature / 100
  coeff > 19 ? 138.5177312231 * Math.log(coeff - 10) - 305.0447927307 : 0
end
calculate_rgb_green_by_color_temperature(color_temperature) click to toggle source
# File lib/yeelight_cli/color_processor.rb, line 41
def calculate_rgb_green_by_color_temperature(color_temperature)
  99.4708025861 * Math.log(color_temperature / 100) - 161.1195681661
end
check_huesat!(hue, sat) click to toggle source
# File lib/yeelight_cli/color_processor.rb, line 36
def check_huesat!(hue, sat)
  raise InvalidArgumentError unless (0..359).cover?(hue)
  raise InvalidArgumentError unless (0..100).cover?(sat)
end
huesat_coeffs_to_rgb_array(vmin, vinc, vdec, hi) click to toggle source

rubocop:disable CyclomaticComplexity rubocop:disable MethodLength rubocop:disable UncommunicativeMethodParamName

# File lib/yeelight_cli/color_processor.rb, line 61
def huesat_coeffs_to_rgb_array(vmin, vinc, vdec, hi)
  case hi
  when 0
    [100, vinc, vmin]
  when 1
    [vdec, 100, vmin]
  when 2
    [vmin, 100, vinc]
  when 3
    [vmin, vdec, 100]
  when 4
    [vinc, vmin, 100]
  when 5
    [100, vmin, vdec]
  end
end