Class: Faker::Color
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
-
.color_name ⇒ String
Produces the name of a color.
-
.hex_color(args = nil) ⇒ String
Produces a hex color code.
-
.hsl_color(hue: nil, saturation: nil, lightness: nil) ⇒ Array(Float, Float, Float)
Produces an array of floats representing an HSL color.
-
.hsla_color ⇒ Array(Float, Float, Float, Float)
Produces an array of floats representing an HSLA color.
-
.rgb_color ⇒ Array(Integer, Integer, Integer)
Produces an array of integers representing an RGB color.
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_name ⇒ String
Produces the name of a color.
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.
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]`.
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_color ⇒ Array(Float, Float, Float, Float)
Produces an array of floats representing an HSLA color. The array is in the form of ‘[hue, saturation, lightness, alpha]`.
105 106 107 |
# File 'lib/faker/default/color.rb', line 105 def hsla_color hsl_color << rand.round(1) end |
.rgb_color ⇒ Array(Integer, Integer, Integer)
Produces an array of integers representing an RGB color.
63 64 65 |
# File 'lib/faker/default/color.rb', line 63 def rgb_color Array.new(3) { single_rgb_color } end |