Class: Faker::Color

Inherits:
Base
  • Object
show all
Defined in:
lib/faker/default/color.rb

Constant Summary collapse

LIGHTNESS_LOOKUP =
{
  light: 0.8,
  dark: 0.2
}.freeze

Constants inherited from Base

Base::LLetters, Base::Letters, Base::NOT_GIVEN, Base::Numbers, Base::ULetters

Class Method Summary collapse

Methods inherited from Base

bothify, disable_enforce_available_locales, fetch, fetch_all, flexible, generate, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, resolve, respond_to_missing?, sample, shuffle, shuffle!, translate, unique, with_locale

Class Method Details

.color_nameString

Produces the name of a color.

Examples:

Faker::Color.color_name #=> "yellow"

Returns:

  • (String)

Available since:

  • 1.6.2



45
46
47
# File 'lib/faker/default/color.rb', line 45

def color_name
  fetch('color.name')
end

.hex_color(args = nil) ⇒ String

Produces a hex color code. Clients are able to specify the hue, saturation, or lightness of the required color. Alternatively a client can simply specify that they need a light or dark color.

Examples:

Faker::Color.hex_color #=> "#31a785"
Faker::Color.hex_color(hue: 118, saturation: 1, lightness: 0.53) #=> "#048700"
Faker::Color.hex_color(:light) #=> "#FFEE99"
Faker::Color.hex_color(:dark) #=> "#665500"

Parameters:

  • args (Hash, Symbol) (defaults to: nil)

    Allows the client to specify what color should be return

Returns:

  • (String)

Available since:

  • next



29
30
31
32
33
34
# File 'lib/faker/default/color.rb', line 29

def hex_color(args = nil)
  hsl_hash = {}
  hsl_hash = { lightness: LIGHTNESS_LOOKUP[args] } if %i[dark light].include?(args)
  hsl_hash = args if args.is_a?(Hash)
  hsl_to_hex(hsl_color(**hsl_hash))
end

.hsl_color(hue: nil, saturation: nil, lightness: nil) ⇒ Array(Float, Float, Float)

Produces an array of floats representing an HSL color. The array is in the form of ‘[hue, saturation, lightness]`.

Examples:

Faker::Color.hsl_color #=> [69.87, 0.66, 0.3]
Faker::Color.hsl_color(hue: 70, saturation: 0.5, lightness: 0.8) #=> [70, 0.5, 0.8]
Faker::Color.hsl_color(hue: 70) #=> [70, 0.66, 0.6]
Faker::Color.hsl_color(saturation: 0.2) #=> [54, 0.2, 0.3]
Faker::Color.hsl_color(lightness: 0.6) #=> [69.87, 0.66, 0.6]

Parameters:

  • hue (FLoat) (defaults to: nil)

    Optional value to use for hue

  • saturation (Float) (defaults to: nil)

    Optional value to use for saturation

  • lightness (Float) (defaults to: nil)

    Optional value to use for lightness

Returns:

  • (Array(Float, Float, Float))

Available since:

  • next



88
89
90
91
92
93
# File 'lib/faker/default/color.rb', line 88

def hsl_color(hue: nil, saturation: nil, lightness: nil)
  valid_hue = hue || sample((0..360).to_a)
  valid_saturation = saturation&.clamp(0, 1) || rand.round(2)
  valid_lightness = lightness&.clamp(0, 1) || rand.round(2)
  [valid_hue, valid_saturation, valid_lightness]
end

.hsla_colorArray(Float, Float, Float, Float)

Produces an array of floats representing an HSLA color. The array is in the form of ‘[hue, saturation, lightness, alpha]`.

Examples:

Faker::Color.hsla_color #=> [154.77, 0.36, 0.9, 0.2]

Returns:

  • (Array(Float, Float, Float, Float))

Available since:

  • 1.5.0



105
106
107
# File 'lib/faker/default/color.rb', line 105

def hsla_color
  hsl_color << rand.round(1)
end

.rgb_colorArray(Integer, Integer, Integer)

Produces an array of integers representing an RGB color.

Examples:

Faker::Color.rgb_color #=> [54, 233, 67]

Returns:

  • (Array(Integer, Integer, Integer))

Available since:

  • 1.5.0



63
64
65
# File 'lib/faker/default/color.rb', line 63

def rgb_color
  Array.new(3) { single_rgb_color }
end