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.
131 132 133 134 135 136 137 138 |
# File 'lib/color_contrast_calc/color.rb', line 131 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.
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/color_contrast_calc/color.rb', line 96 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 if color_value.is_a?(Array) return color_from_rgba(color_value, name) if color_value.length == 4 return color_from_rgb(color_value, name) end return color_from_func(color_value, name) if function?(color_value) color_from_str(color_value, name) end |
#from_hex(hex, name = nil) ⇒ Color
Return an instance of Color for a hex color code.
58 59 60 61 |
# File 'lib/color_contrast_calc/color.rb', line 58 def from_hex(hex, name = nil) normalized_hex = Utils.normalize_hex(hex) !name && List::HEX_TO_COLOR[normalized_hex] || new(normalized_hex, name) end |
#from_hsl(hsl, name = nil) ⇒ Color
Return an instance of Color from an HSL value.
70 71 72 73 74 75 76 77 78 |
# File 'lib/color_contrast_calc/color.rb', line 70 def from_hsl(hsl, name = nil) if hsl.length == 4 rgb = Utils.hsl_to_rgb(hsl[0, 3]) return new(rgb.push(hsl.last), name) unless opaque?(hsl) end rgb ||= Utils.hsl_to_rgb(hsl) !name && List::HEX_TO_COLOR[Utils.rgb_to_hex(rgb)] || new(rgb, name) end |
#from_name(name) ⇒ Color
Return an instance of Color for a predefined color name.
Color names are defined at
35 36 37 |
# File 'lib/color_contrast_calc/color.rb', line 35 def from_name(name) List::NAME_TO_COLOR[name.downcase] end |
#from_rgb(rgb, name = nil) ⇒ Color
Return an instance of Color for an RGB value
47 48 49 |
# File 'lib/color_contrast_calc/color.rb', line 47 def from_rgb(rgb, name = nil) !name && List::HEX_TO_COLOR[Utils.rgb_to_hex(rgb)] || new(rgb, name) end |