From 0a7fa3782b596ff0aed865359d0feab6bf1bfea7 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Wed, 6 Apr 2016 15:50:30 +0800 Subject: [PATCH] Feature option to change color theme * Add `config.style` option, to allow use change render style. --- CHANGELOG.md | 5 +++++ Gemfile.lock | 2 +- README.md | 2 ++ lib/rucaptcha.rb | 1 + lib/rucaptcha/captcha.rb | 11 ++++++++--- lib/rucaptcha/configuration.rb | 2 ++ lib/rucaptcha/version.rb | 2 +- spec/captcha_spec.rb | 18 ++++++++++++++++++ 8 files changed, 38 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d8aa907..641e05a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +0.4.0 +----- + +- Add `config.colorize` option, to allow use black text theme. + 0.3.3 ----- diff --git a/Gemfile.lock b/Gemfile.lock index b3e0ce4..b0fb3cb 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - rucaptcha (0.3.3) + rucaptcha (0.4.0) posix-spawn (>= 0.3.0) GEM diff --git a/README.md b/README.md index 09ea4b0..f101fb9 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,8 @@ RuCaptcha.configure do self.cache_limit = 100 # Custom captcha code expire time if you need, default: 2 minutes # self.expires_in = 120 + # Color style, default: :colorful, allows: [:colorful, :black_white] + # self.style = :colorful end ``` diff --git a/lib/rucaptcha.rb b/lib/rucaptcha.rb index e444f46..8275aae 100644 --- a/lib/rucaptcha.rb +++ b/lib/rucaptcha.rb @@ -19,6 +19,7 @@ module RuCaptcha @config.implode = 0.4 @config.cache_limit = 100 @config.expires_in = 2.minutes + @config.style = :colorful @config end diff --git a/lib/rucaptcha/captcha.rb b/lib/rucaptcha/captcha.rb index 34f8e3e..88aa1c8 100644 --- a/lib/rucaptcha/captcha.rb +++ b/lib/rucaptcha/captcha.rb @@ -3,8 +3,13 @@ require 'posix-spawn' module RuCaptcha class Captcha class << self - def rand_color - [rand(100).to_s(8), rand(100).to_s(8), rand(100).to_s(8)] + def random_color + if RuCaptcha.config.style == :colorful + [rand(100).to_s(8), rand(100).to_s(8), rand(100).to_s(8)] + else + color_seed = rand(50).to_s(8) + [color_seed, color_seed, color_seed] + end end def random_chars @@ -37,7 +42,7 @@ module RuCaptcha text_opts = [] line_opts = [] chars.each_with_index do |char, i| - rgb = rand_color + rgb = random_color text_color = "rgba(#{rgb.join(',')}, 1)" line_color = "rgba(#{rgb.join(',')}, 0.6)" text_opts << %(-fill '#{text_color}' -draw 'text #{(text_left + text_width) * i + all_left},#{text_top} "#{char}"') diff --git a/lib/rucaptcha/configuration.rb b/lib/rucaptcha/configuration.rb index 394f0da..3286a14 100644 --- a/lib/rucaptcha/configuration.rb +++ b/lib/rucaptcha/configuration.rb @@ -11,6 +11,8 @@ module RuCaptcha # Number of Captcha codes limit # set 0 to disable limit and file cache, default: 100 attr_accessor :cache_limit + # Color style, default: :colorful, allows: [:colorful, :black_white] + attr_accessor :style # session[:_rucaptcha] expire time, default 2 minutes attr_accessor :expires_in end diff --git a/lib/rucaptcha/version.rb b/lib/rucaptcha/version.rb index 10e2368..66781cf 100644 --- a/lib/rucaptcha/version.rb +++ b/lib/rucaptcha/version.rb @@ -1,3 +1,3 @@ module RuCaptcha - VERSION = '0.3.3' + VERSION = '0.4.0' end diff --git a/spec/captcha_spec.rb b/spec/captcha_spec.rb index a67a9a4..6b971f6 100644 --- a/spec/captcha_spec.rb +++ b/spec/captcha_spec.rb @@ -16,4 +16,22 @@ describe RuCaptcha::Captcha do end end end + + describe '.random_color' do + it 'should return colorful array' do + allow(RuCaptcha.config).to receive(:style).and_return(:colorful) + colors = RuCaptcha::Captcha.random_color + expect(colors.uniq.size >= 2).to eq true + colors1 = RuCaptcha::Captcha.random_color + expect(colors).not_to eq colors1 + end + + it 'should return black color array' do + allow(RuCaptcha.config).to receive(:style).and_return(:black_white) + colors = RuCaptcha::Captcha.random_color + expect(colors.uniq.size).to eq 1 + colors1 = RuCaptcha::Captcha.random_color + expect(colors).not_to eq colors1 + end + end end