Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue-2464: Add color luminosity options #2566

Merged
merged 11 commits into from
Sep 26, 2022
7 changes: 6 additions & 1 deletion lib/faker/default/color.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ class << self
}.freeze
##
# 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.
#
# @param args [Symbol] Allows the client to specify light or dark colors
# @param args [Hash, Symbol] Allows the client to specify what color should be return
Zeragamba marked this conversation as resolved.
Show resolved Hide resolved
#
# @return [String]
#
# @example
# Faker::Color.hex_color #=> "#31a785"
# @example
# Faker::Color.hex_color(hue: 118, saturation: 1,lightness: 0.53) #=> "#048700"
Zeragamba marked this conversation as resolved.
Show resolved Hide resolved
# @example
# Faker::Color.hex_color(:light) #=> "#FFEE99"
# @example
# Faker::Color.hex_color(:dark) #=> "#665500"
Expand All @@ -25,6 +29,7 @@ class << self
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

Expand Down
13 changes: 13 additions & 0 deletions test/faker/default/test_faker_color.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ def test_hex_color_dark
assert_in_delta(0.2, helper_hex_lightness(@tester.hex_color(:dark)))
end

def test_hex_color_with_hash_is_passed_to_hsl_color
mock_hsl_color = lambda do |args|
assert_equal(100, args[:hue])
assert_in_delta(0.2, args[:saturation])
assert_in_delta(0.8, args[:lightness])
[args[:hue], args[:saturation], args[:lightness]]
end
@tester.stub :hsl_color, mock_hsl_color do
result = @tester.hex_color(hue: 100, saturation: 0.2, lightness: 0.8)
assert_match(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/, result)
end
end

def test_single_rgb_color
assert @tester.single_rgb_color.between?(0, 255)
end
Expand Down