Skip to content

Commit

Permalink
Random color with Placeholdit (#1179)
Browse files Browse the repository at this point in the history
* Add option to use random color for Placeholdit

If :random given as value for background_color or text_color in
Placeholdit module, it will generate a random hex color.

* Update Placeholdit documentation

* Update Changelog
  • Loading branch information
nicolas-brousse authored and vbrazo committed May 18, 2018
1 parent 0d9636d commit 42428f4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Change Log

## HEAD Unreleased
### Latest update: 2018-05-17
### Latest update: 2018-05-18

**Additions**

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions doc/placeholdit.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'"
```
9 changes: 9 additions & 0 deletions lib/faker/placeholdit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)/)
Expand All @@ -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
8 changes: 8 additions & 0 deletions test/test_placeholdit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand Down

0 comments on commit 42428f4

Please sign in to comment.