Module: ColorContrastCalc::Color::Factory
- Includes:
- Deprecated::Color::Factory
- Included in:
- ColorContrastCalc::Color
- Defined in:
- lib/color_contrast_calc/color.rb
Overview
Module that implements class methods of Color
Instance Method Summary collapse
-
#as_color(color_value, name = nil) ⇒ Color
Return an instance of Color.
-
#color_from(color_value, name = nil) ⇒ Color
Return an instance of Color.
-
#from_hex(hex, name = nil) ⇒ Color
Return an instance of Color for a hex color code.
-
#from_hsl(hsl, name = nil) ⇒ Color
Return an instance of Color from an HSL value.
-
#from_name(name) ⇒ Color
Return an instance of Color for a predefined color name.
-
#from_rgb(rgb, name = nil) ⇒ Color
Return an instance of Color for an RGB value.
Methods included from Deprecated::Color::Factory
Instance Method Details
#as_color(color_value, name = nil) ⇒ Color
Return an instance of Color.
As color_value
, you can pass a Color instance, a predefined color name, an RGB value represented as an array of integers like [255, 255, 0], or a string such as a hex code like “#ffff00”.
+name+ is assigned to the returned instance.
122 123 124 125 126 127 128 129 |
# File 'lib/color_contrast_calc/color.rb', line 122 def as_color(color_value, name = nil) if color_value.is_a? Color return color_value if color_value.name == name color_value = color_value.rgb end color_from(color_value, name) end |
#color_from(color_value, name = nil) ⇒ Color
Return an instance of Color.
As color_value
, you can pass a predefined color name, an RGB value represented as an array of integers like [255, 255, 0], or a string such as a hex code like “#ffff00”. name
is assigned to the returned instance.
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/color_contrast_calc/color.rb', line 94 def color_from(color_value, name = nil) if !color_value.is_a?(String) && !color_value.is_a?(Array) raise InvalidColorRepresentationError.from_value(color_value) end return color_from_rgb(color_value, name) if color_value.is_a?(Array) if /\A(?:rgb|hsl|hwb)/i =~ color_value return color_from_func(color_value, name) end color_from_str(color_value, name) end |
#from_hex(hex, name = nil) ⇒ Color
Return an instance of Color for a hex color code.
59 60 61 62 63 |
# File 'lib/color_contrast_calc/color.rb', line 59 def from_hex(hex, name = nil) normalized_hex = Utils.normalize_hex(hex) !name && List::HEX_TO_COLOR[normalized_hex] || Color.new(normalized_hex, name) end |
#from_hsl(hsl, name = nil) ⇒ Color
Return an instance of Color from an HSL value.
72 73 74 75 76 |
# File 'lib/color_contrast_calc/color.rb', line 72 def from_hsl(hsl, name = nil) rgb = Utils.hsl_to_rgb(hsl) !name && List::HEX_TO_COLOR[Utils.rgb_to_hex(rgb)] || Color.new(rgb, name) end |