diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d39aa917e..9aab46e8e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ # Change Log ## HEAD Unreleased -### Latest update: 2018-05-17 +### Latest update: 2018-05-18 **Additions** @@ -37,6 +37,7 @@ - [PR #1173](https://github.com/stympy/faker/pull/1173) Fix tests warning [@vbrazo](https://github.com/vbrazo) - [PR #1193](https://github.com/stympy/faker/pull/1193) Add Faker::MichaelScott API [@snayrouz](https://github.com/snayrouz) - [PR #818](https://github.com/stympy/faker/pull/818) LoremFlickr support [@mrstebo](https://github.com/mrstebo) +- [PR #1179](https://github.com/stympy/faker/pull/1179) Random color with Placeholdit [@nicolas-brousse](https://github.com/nicolas-brousse) - New collaborator - Vitor Oliveira [@vbrazo](https://github.com/vbrazo) ## [v1.8.7](https://github.com/stympy/faker/tree/v1.8.7) (2017-12-22) diff --git a/doc/placeholdit.md b/doc/placeholdit.md index b776fbacf8..ca23bca037 100644 --- a/doc/placeholdit.md +++ b/doc/placeholdit.md @@ -9,7 +9,11 @@ Faker::Placeholdit.image("50x50", 'jpg') #=> "http://placehold.it/50x50.jpg/000" Faker::Placeholdit.image("50x50", 'gif', 'ffffff') #=> "http://placehold.it/50x50.gif/ffffff" +Faker::Placeholdit.image("50x50", 'jpeg', :random) #=> "http://placehold.it/50x50.jpeg/39eba7" + Faker::Placeholdit.image("50x50", 'jpeg', 'ffffff', '000') #=> "http://placehold.it/50x50.jpeg/ffffff/000" +Faker::Placeholdit.image("50x50", 'jpeg', :random, :random) #=> "http://placehold.it/50x50.jpeg/d39e44/888ba7" + Faker::Placeholdit.image("50x50", 'jpg', 'ffffff', '000', 'Some Custom Text') #=> "http://placehold.it/50x50.jpg/ffffff/000?text='Some Custom Text'" ``` diff --git a/lib/faker/placeholdit.rb b/lib/faker/placeholdit.rb index fdaa25beea..319b974674 100644 --- a/lib/faker/placeholdit.rb +++ b/lib/faker/placeholdit.rb @@ -4,6 +4,9 @@ class << self SUPPORTED_FORMATS = %w[png jpg gif jpeg].freeze def image(size = '300x300', format = 'png', background_color = nil, text_color = nil, text = nil) + background_color = generate_color if background_color == :random + text_color = generate_color if text_color == :random + raise ArgumentError, 'Size should be specified in format 300x300' unless size =~ /^[0-9]+x[0-9]+$/ raise ArgumentError, "Supported formats are #{SUPPORTED_FORMATS.join(', ')}" unless SUPPORTED_FORMATS.include?(format) raise ArgumentError, "background_color must be a hex value without '#'" unless background_color.nil? || background_color.match(/((?:^\h{3}$)|(?:^\h{6}$)){1}(?!.*\H)/) @@ -15,6 +18,12 @@ def image(size = '300x300', format = 'png', background_color = nil, text_color = image_url += "?text=#{text}" if text image_url end + + private + + def generate_color + format('%06x', (rand * 0xffffff)) + end end end end diff --git a/test/test_placeholdit.rb b/test/test_placeholdit.rb index afe8efabec..e17eff0206 100644 --- a/test/test_placeholdit.rb +++ b/test/test_placeholdit.rb @@ -37,6 +37,10 @@ def test_avatar_background_with_correct_three_char_hex assert @tester.image('300x300', 'jpg', 'fff').match(%r{https:\/\/placehold\.it\/(.+)(jpg?)\/fff}) end + def test_avatar_background_with_random_color + assert @tester.image('300x300', 'jpg', :random).match(%r{https:\/\/placehold\.it\/(.+)(jpg?)\/[a-f0-9]{6}}) + end + def test_avatar_background_with_wrong_six_char_hex assert_raise ArgumentError do @tester.image('300x300', 'jpg', 'fffffz') @@ -63,6 +67,10 @@ def test_avatar_font_color_with_correct_three_char_hex assert @tester.image('300x300', 'jpg', 'fff', '000').match(%r{https:\/\/placehold\.it\/(.+)(jpg?)\/fff}) end + def test_avatar_font_color_with_random_color + assert @tester.image('300x300', 'jpg', 'fff', :random).match(%r{https:\/\/placehold\.it\/(.+)(jpg?)\/fff\/[a-f0-9]{6}}) + end + def test_avatar_font_color_with_wrong_six_char_hex assert_raise ArgumentError do @tester.image('300x300', 'jpg', 'ffffff', '900F0z')