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



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.

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



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.

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



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.

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



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

#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
50
# 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)] ||
    Color.new(rgb, name)
end