Class: Faker::Color

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

Constant Summary

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, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, resolve, respond_to_missing?, sample, 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


28
29
30
# File 'lib/faker/default/color.rb', line 28

def color_name
  fetch('color.name')
end

.hex_colorString

Produces a hex color code.

Examples:

Faker::Color.hex_color #=> "#31a785"

Returns:

  • (String)

Available since:

  • 1.5.0


15
16
17
# File 'lib/faker/default/color.rb', line 15

def hex_color
  format('#%06x', (rand * 0xffffff))
end

.hsl_colorArray(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]

Returns:

  • (Array(Float, Float, Float))

Available since:

  • 1.5.0


60
61
62
# File 'lib/faker/default/color.rb', line 60

def hsl_color
  [sample((0..360).to_a), rand.round(2), rand.round(2)]
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


74
75
76
# File 'lib/faker/default/color.rb', line 74

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


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

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