Class: Chroma::Converters::HsvConverter

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

Overview

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

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::Hsv

Convert hsl to hsv.


37
38
39
# File 'lib/chroma/converters/hsv_converter.rb', line 37

def convert_hsl
  HslConverter.convert_rgb(RgbConverter.convert_hsv(@input))
end

#convert_hsvColorModes::Hsv

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


43
44
45
# File 'lib/chroma/converters/hsv_converter.rb', line 43

def convert_hsv
  @input
end

#convert_rgbColorModes::Hsv

Convert rgb to hsv.


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/chroma/converters/hsv_converter.rb', line 7

def convert_rgb
  r = bound01(@input.r, 255)
  g = bound01(@input.g, 255)
  b = bound01(@input.b, 255)

  rgb_array = [r, g, b]

  max = rgb_array.max
  min = rgb_array.min
  v = max
  d = (max - min).to_f
  s = max.zero? ? 0 : d / max

  if max == min
    h = 0
  else
    h = case max
        when r then (g - b) / d + (g < b ? 6 : 0)
        when g then (b - r) / d + 2
        when b then (r - g) / d + 4
        end

    h /= 6.0
  end

  ColorModes::Hsv.new(h * 360, s, v, @input.a)
end