Class: ColorConverters::BaseConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/color_converters/base_converter.rb

Constant Summary collapse

IMPORT_DP =
8
OUTPUT_DP =
2

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(colour_input, limit_override = false, limit_clamp = false) ⇒ BaseConverter

Returns a new instance of BaseConverter.

Raises:



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/color_converters/base_converter.rb', line 30

def initialize(colour_input, limit_override = false, limit_clamp = false)
  colour_input.delete(:space) if colour_input.is_a?(Hash)

  # colour_input = self.clamp_input(colour_input) if limit_clamp == true

  validation_errors = self.validate_input(colour_input) # validation method is defined in each convertor
  raise InvalidColorError, "Invalid color input: #{validation_errors.join(', ')}" if limit_override == false && validation_errors.present?

  r, g, b, a = self.input_to_rgba(colour_input) # conversion method is defined in each convertor

  @rgba = { r: r.to_f.round(IMPORT_DP), g: g.to_f.round(IMPORT_DP), b: b.to_f.round(IMPORT_DP), a: a.to_f.round(IMPORT_DP) }
end

Class Attribute Details

.convertersObject (readonly)

Returns the value of attribute converters.



16
17
18
# File 'lib/color_converters/base_converter.rb', line 16

def converters
  @converters
end

Instance Attribute Details

#original_valueObject (readonly)

Returns the value of attribute original_value.



12
13
14
# File 'lib/color_converters/base_converter.rb', line 12

def original_value
  @original_value
end

#rgbaObject (readonly)

Returns the value of attribute rgba.



12
13
14
# File 'lib/color_converters/base_converter.rb', line 12

def rgba
  @rgba
end

Class Method Details

.factory(colour_input, limit_override = false, limit_clamp = false) ⇒ Object



25
26
27
28
# File 'lib/color_converters/base_converter.rb', line 25

def self.factory(colour_input, limit_override = false, limit_clamp = false)
  converter = BaseConverter.converters.find { |klass| klass.matches?(colour_input) }
  converter&.new(colour_input, limit_override, limit_clamp)
end

.inherited(subclass) ⇒ Object



21
22
23
# File 'lib/color_converters/base_converter.rb', line 21

def self.inherited(subclass)
  BaseConverter.converters << subclass
end

Instance Method Details

#alphaObject



107
108
109
# File 'lib/color_converters/base_converter.rb', line 107

def alpha
  @rgba[:a]
end

#cielabObject



83
84
85
86
87
# File 'lib/color_converters/base_converter.rb', line 83

def cielab
  l, a, b = CielabConverter.xyz_to_cielab(XyzConverter.rgb_to_xyz(self.rgb_array_frac))

  { l: l.to_f.round(OUTPUT_DP), a: a.to_f.round(OUTPUT_DP), b: b.to_f.round(OUTPUT_DP) }
end

#cielchObject



89
90
91
92
93
# File 'lib/color_converters/base_converter.rb', line 89

def cielch
  l, c, h = CielchConverter.cielab_to_cielch(CielabConverter.xyz_to_cielab(XyzConverter.rgb_to_xyz(self.rgb_array_frac)))

  { l: l.to_f.round(OUTPUT_DP), c: c.to_f.round(OUTPUT_DP), h: h.to_f.round(OUTPUT_DP) }
end

#cmykObject



71
72
73
74
75
# File 'lib/color_converters/base_converter.rb', line 71

def cmyk
  c, m, y, k = CmykConverter.rgb_to_cmyk(self.rgb_array_frac)

  { c: c.to_f.round(OUTPUT_DP), m: m.to_f.round(OUTPUT_DP), y: y.to_f.round(OUTPUT_DP), k: k.to_f.round(OUTPUT_DP) }
end

#hexObject



47
48
49
# File 'lib/color_converters/base_converter.rb', line 47

def hex
  HexConverter.rgb_to_hex(self.rgb_array)
end

#hsbObject



65
66
67
68
69
# File 'lib/color_converters/base_converter.rb', line 65

def hsb
  hsb_hash = self.hsv
  hsb_hash[:b] = hsb_hash.delete(:v)
  hsb_hash
end

#hslObject

not refactored to SubClass methods due to needing so many of the private methods



52
53
54
55
56
# File 'lib/color_converters/base_converter.rb', line 52

def hsl
  @r, @g, @b = self.rgb_array_frac

  { h: self.hue.to_f.round(OUTPUT_DP), s: self.hsl_saturation.to_f.round(OUTPUT_DP), l: self.hsl_lightness.to_f.round(OUTPUT_DP) }
end

#hsvObject

not refactored to SubClass methods due to needing so many of the private methods



59
60
61
62
63
# File 'lib/color_converters/base_converter.rb', line 59

def hsv
  @r, @g, @b = self.rgb_array

  { h: self.hue.to_f.round(OUTPUT_DP), s: self.hsv_saturation.to_f.round(OUTPUT_DP), v: self.hsv_value.to_f.round(OUTPUT_DP) }
end

#name(fuzzy: false) ⇒ Object



111
112
113
# File 'lib/color_converters/base_converter.rb', line 111

def name(fuzzy: false)
  NameConverter.rgb_to_name(self.rgb_array, fuzzy)
end

#oklabObject



95
96
97
98
99
# File 'lib/color_converters/base_converter.rb', line 95

def oklab
  l, a, b = OklabConverter.xyz_to_oklab(XyzConverter.rgb_to_xyz(self.rgb_array_frac))

  { l: l.to_f.round(OUTPUT_DP), a: a.to_f.round(OUTPUT_DP), b: b.to_f.round(OUTPUT_DP) }
end

#oklchObject



101
102
103
104
105
# File 'lib/color_converters/base_converter.rb', line 101

def oklch
  l, c, h = OklchConverter.oklab_to_oklch(OklabConverter.xyz_to_oklab(XyzConverter.rgb_to_xyz(self.rgb_array_frac)))

  { l: l.to_f.round(OUTPUT_DP), c: c.to_f.round(OUTPUT_DP), h: h.to_f.round(OUTPUT_DP) }
end

#rgbObject



43
44
45
# File 'lib/color_converters/base_converter.rb', line 43

def rgb
  { r: @rgba[:r].to_f.round(OUTPUT_DP), g: @rgba[:g].to_f.round(OUTPUT_DP), b: @rgba[:b].to_f.round(OUTPUT_DP) }
end

#xyzObject



77
78
79
80
81
# File 'lib/color_converters/base_converter.rb', line 77

def xyz
  x, y, z = XyzConverter.rgb_to_xyz(self.rgb_array_frac)

  { x: x.to_f.round(OUTPUT_DP), y: y.to_f.round(OUTPUT_DP), z: z.to_f.round(OUTPUT_DP) }
end