Class: ColorConverters::OklchConverter

Inherits:
BaseConverter show all
Defined in:
lib/color_converters/converters/oklch_converter.rb

Constant Summary

Constants inherited from BaseConverter

BaseConverter::IMPORT_DP, BaseConverter::OUTPUT_DP

Instance Attribute Summary

Attributes inherited from BaseConverter

#original_value, #rgba

Class Method Summary collapse

Methods inherited from BaseConverter

#alpha, #cielab, #cielch, #cmyk, factory, #hex, #hsb, #hsl, #hsv, inherited, #initialize, #name, #oklab, #oklch, #rgb, #xyz

Constructor Details

This class inherits a constructor from ColorConverters::BaseConverter

Class Method Details

.boundsObject



11
12
13
# File 'lib/color_converters/converters/oklch_converter.rb', line 11

def self.bounds
  { l: [0.0, 100.0], c: [0.0, 500.0], h: [0.0, 360.0] }
end

.matches?(colour_input) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
8
9
# File 'lib/color_converters/converters/oklch_converter.rb', line 5

def self.matches?(colour_input)
  return false unless colour_input.is_a?(Hash)

  colour_input.keys - [:l, :c, :h, :space] == [] && colour_input[:space].to_s == 'ok'
end

.oklab_to_oklch(lab_array) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/color_converters/converters/oklch_converter.rb', line 48

def self.oklab_to_oklch(lab_array)
  l, aa, bb = lab_array.map(&:to_d)

  e = 0.000015.to_d; # if chroma is smaller than this, set hue to 0 similar to CIELch

  c = ((aa**2.to_d) + (bb**2.to_d))**0.5.to_d

  h_rad = Math.atan2(bb, aa).to_d
  h = h_rad * (180.0.to_d / Math::PI.to_d)

  h %= 360

  h = 0 if c < e

  [l, c, h]
end

.oklch_to_oklab(colour_input) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/color_converters/converters/oklch_converter.rb', line 35

def self.oklch_to_oklab(colour_input)
  l = colour_input[:l].to_d
  c = colour_input[:c].to_d
  h = colour_input[:h].to_d

  h_rad = h * (Math::PI.to_d / 180.0.to_d)

  a = c * Math.cos(h_rad).to_d
  b = c * Math.sin(h_rad).to_d

  [l, a, b]
end