class Faker::Color
def color_name
-
(String)
-
def color_name fetch('color.name') end
def hex_color(args = nil)
-
(String)
-
Parameters:
-
args
(Hash, Symbol
) -- Allows the client to specify what color should be return
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
def hsl_color(hue: nil, saturation: nil, lightness: nil)
-
(Array(Float, Float, Float))
-
Parameters:
-
lightness
(Float
) -- Optional value to use for lightness -
saturation
(Float
) -- Optional value to use for saturation -
hue
(FLoat
) -- Optional value to use for hue
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
def hsl_to_hex(a_hsl_color)
- See: https://github.com/jpmckinney/color-generator/blob/master/lib/color-generator.rb -
See: https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB -
Returns:
-
(String)
-
Parameters:
-
a_hsl_color
(Array(Float, Float, Float)
) -- The array that represents the HSL color
def hsl_to_hex(a_hsl_color) h, s, l = a_hsl_color c = (1 - (2 * l - 1).abs) * s h_prime = h / 60 x = c * (1 - (h_prime % 2 - 1).abs) m = l - 0.5 * c rgb = case h_prime.to_i when 0 # 0 <= H' < 1 [c, x, 0] when 1 # 1 <= H' < 2 [x, c, 0] when 2 # 2 <= H' < 3 [0, c, x] when 3 # 3 <= H' < 4 [0, x, c] when 4 # 4 <= H' < 5 [x, 0, c] else # 5 <= H' < 6 [c, 0, x] end.map { |value| ((value + m) * 255).round } format('#%02x%02x%02x', rgb[0], rgb[1], rgb[2]) end
def hsla_color
-
(Array(Float, Float, Float, Float))
-
def hsla_color hsl_color << rand.round(1) end
def rgb_color
-
(Array(Integer, Integer, Integer))
-
def rgb_color Array.new(3) { single_rgb_color } end
def single_rgb_color
- Private: -
def single_rgb_color sample((0..255).to_a) end