Module: Sai::Conversion::RGB::ColorSpace Private

Defined in:
lib/sai/conversion/rgb/color_space.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Convert colors between different color space formats

Author:

Since:

  • 0.3.1

Class Method Summary collapse

Class Method Details

.hsv_to_rgb(hue, saturation, value) ⇒ Array<Integer>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Convert HSV values to RGB

Parameters:

  • hue (Float) —

    the hue component (0-360)

  • saturation (Float) —

    the saturation component (0-1)

  • value (Float) —

    the value component (0-1)

Returns:

  • (Array<Integer>) —

    the RGB values

Author:

Since:

  • 0.3.1



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sai/conversion/rgb/color_space.rb', line 29

def hsv_to_rgb(hue, saturation, value)
  hue_sector = (hue / 60.0).floor.to_i
  hue_remainder = (hue / 60.0) - hue_sector

  components = calculate_hsv_components(value, saturation, hue_remainder)
  primary, secondary, tertiary = *components

  return [0, 0, 0] unless primary && secondary && tertiary

  rgb = select_rgb_values(hue_sector, value, primary, secondary, tertiary)
  normalize_rgb(rgb)
end

.resolve(color) ⇒ Array<Integer>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Convert a color value to RGB components

Parameters:

  • color (String, Array<Integer>) —

    the color to convert

Returns:

  • (Array<Integer>) —

    the RGB components

Raises:

  • (ArgumentError) —

    if the color format is invalid

Author:

Since:

  • 0.1.0



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sai/conversion/rgb/color_space.rb', line 54

def resolve(color)
  case color
  when Array then validate_rgb(color)
  when /^#?([A-Fa-f0-9]{6})$/
    hex_to_rgb(
      Regexp.last_match(1) # steep:ignore ArgumentTypeMismatch
    )
  when String, Symbol then named_to_rgb(color.to_s.downcase)
  else
    raise ArgumentError, "Invalid color format: #{color}"
  end
end