Class: Chroma::Converters::RgbConverter

Inherits:
Base
  • Object
show all
Defined in:
lib/chroma/converters/rgb_converter.rb

Overview

Class to convert a color mode to Chroma::ColorModes::Rgb.

Instance Method Summary collapse

Methods inherited from Base

convert_hsl, convert_hsv, convert_rgb, #initialize

Methods included from Helpers::Bounders

#bound01, #bound_alpha, #clamp01, #to_percentage

Constructor Details

This class inherits a constructor from Chroma::Converters::Base

Instance Method Details

#convert_hslColorModes::Rgb

Convert hsl to rgb.

Returns:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/chroma/converters/rgb_converter.rb', line 13

def convert_hsl
  h, s, l = @input

  h = bound01(h, 360)
  s = bound01(s, 100)
  l = bound01(l, 100)

  if s.zero?
    r = g = b = l * 255
  else
    q = l < 0.5 ? l * (1 + s) : l + s - l * s
    p = 2 * l - q
    r = hue_to_rgb(p, q, h + 1/3.0) * 255
    g = hue_to_rgb(p, q, h) * 255
    b = hue_to_rgb(p, q, h - 1/3.0) * 255
  end

  ColorModes::Rgb.new(r, g, b, bound_alpha(@input.a))
end

#convert_hsvColorModes::Rgb

Convert hsv to rgb.

Returns:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/chroma/converters/rgb_converter.rb', line 35

def convert_hsv
  h, s, v = @input

  h = bound01(h, 360) * 6
  s = bound01(s, 100)
  v = bound01(v, 100)

  i = h.floor
  f = h - i
  p = v * (1 - s)
  q = v * (1 - f * s)
  t = v * (1 - (1 - f) * s)
  mod = i % 6

  r = [v, q, p, p, t, v][mod] * 255
  g = [t, v, v, q, p, p][mod] * 255
  b = [p, p, t, v, v, q][mod] * 255

  ColorModes::Rgb.new(r, g, b, bound_alpha(@input.a))
end

#convert_rgbColorModes::Rgb

Returns @input because it's the same color mode.

Returns:



7
8
9
# File 'lib/chroma/converters/rgb_converter.rb', line 7

def convert_rgb
  @input
end