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

Methods included from Deprecated::Color::Factory

#new_from_hsl

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.

Parameters:

  • color_value (String, Array<Integer>)

    An instance of Color, a predefined color name, hex color code, rgb/hsl/hwb functions or RGB value

  • name (String) (defaults to: nil)

    Without specifying a name, a color keyword name (if exists) or the value of normalized hex color code is assigned to Color#name

Returns:

  • (Color)

    Instance of Color



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.

Parameters:

  • color_value (String, Array<Integer>)

    Name of a predefined color, hex color code, rgb/hsl/hwb functions or RGB value. Yellow, for example, can be given as [255, 255, 0], “#ffff00”, “rgb(255, 255, 255)”, “hsl(60deg, 100% 50%)” or “hwb(60deg 0% 0%)”.

  • name (String) (defaults to: nil)

    Without specifying a name, a color keyword name (if exists) or the value of normalized hex color code is assigned to Color#name

Returns:

  • (Color)

    Instance of Color



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.

Parameters:

  • hex (String)

    Hex color code such as “#ffff00”

  • name (String) (defaults to: nil)

    You can name the color to be created

Returns:

  • (Color)

    Instance of Color



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.

Parameters:

  • hsl (Float)

    HSL value represented as an array of numbers

  • name (String) (defaults to: nil)

    You can name the color to be created

Returns:

  • (Color)

    Instance of Color



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

Parameters:

  • name (String)

    Name of color

Returns:

  • (Color)

    Instance of Color



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

Parameters:

  • rgb (Array<Integer>)

    RGB value represented as an array of integers such as [255, 255, 0]

  • name (String) (defaults to: nil)

    You can name the color to be created

Returns:

  • (Color)

    Instance of Color



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