Class: ColorConverters::BaseConverter
- Inherits:
-
Object
- Object
- ColorConverters::BaseConverter
show all
- Defined in:
- lib/color_converters/base_converter.rb
Direct Known Subclasses
CielabConverter, CielchConverter, CmykConverter, HexConverter, HslConverter, HslStringConverter, HsvConverter, NameConverter, NullConverter, OklabConverter, OklchConverter, RgbConverter, RgbStringConverter, XyzConverter
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.
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)
validation_errors = self.validate_input(colour_input) 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)
@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
.converters ⇒ Object
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_value ⇒ Object
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
|
#rgba ⇒ Object
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
#alpha ⇒ Object
107
108
109
|
# File 'lib/color_converters/base_converter.rb', line 107
def alpha
@rgba[:a]
end
|
#hex ⇒ Object
47
48
49
|
# File 'lib/color_converters/base_converter.rb', line 47
def hex
HexConverter.rgb_to_hex(self.rgb_array)
end
|
#hsb ⇒ Object
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
|
#hsl ⇒ Object
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
|
#hsv ⇒ Object
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
|
#rgb ⇒ Object
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
|