Feature option to change color theme

* Add `config.style` option, to allow use change render style.
This commit is contained in:
Jason Lee 2016-04-06 15:50:30 +08:00
parent d5a0ecc6ac
commit 0a7fa3782b
8 changed files with 38 additions and 5 deletions

View File

@ -1,3 +1,8 @@
0.4.0
-----
- Add `config.colorize` option, to allow use black text theme.
0.3.3 0.3.3
----- -----

View File

@ -1,7 +1,7 @@
PATH PATH
remote: . remote: .
specs: specs:
rucaptcha (0.3.3) rucaptcha (0.4.0)
posix-spawn (>= 0.3.0) posix-spawn (>= 0.3.0)
GEM GEM

View File

@ -67,6 +67,8 @@ RuCaptcha.configure do
self.cache_limit = 100 self.cache_limit = 100
# Custom captcha code expire time if you need, default: 2 minutes # Custom captcha code expire time if you need, default: 2 minutes
# self.expires_in = 120 # self.expires_in = 120
# Color style, default: :colorful, allows: [:colorful, :black_white]
# self.style = :colorful
end end
``` ```

View File

@ -19,6 +19,7 @@ module RuCaptcha
@config.implode = 0.4 @config.implode = 0.4
@config.cache_limit = 100 @config.cache_limit = 100
@config.expires_in = 2.minutes @config.expires_in = 2.minutes
@config.style = :colorful
@config @config
end end

View File

@ -3,8 +3,13 @@ require 'posix-spawn'
module RuCaptcha module RuCaptcha
class Captcha class Captcha
class << self class << self
def rand_color def random_color
if RuCaptcha.config.style == :colorful
[rand(100).to_s(8), rand(100).to_s(8), rand(100).to_s(8)] [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 end
def random_chars def random_chars
@ -37,7 +42,7 @@ module RuCaptcha
text_opts = [] text_opts = []
line_opts = [] line_opts = []
chars.each_with_index do |char, i| chars.each_with_index do |char, i|
rgb = rand_color rgb = random_color
text_color = "rgba(#{rgb.join(',')}, 1)" text_color = "rgba(#{rgb.join(',')}, 1)"
line_color = "rgba(#{rgb.join(',')}, 0.6)" line_color = "rgba(#{rgb.join(',')}, 0.6)"
text_opts << %(-fill '#{text_color}' -draw 'text #{(text_left + text_width) * i + all_left},#{text_top} "#{char}"') text_opts << %(-fill '#{text_color}' -draw 'text #{(text_left + text_width) * i + all_left},#{text_top} "#{char}"')

View File

@ -11,6 +11,8 @@ module RuCaptcha
# Number of Captcha codes limit # Number of Captcha codes limit
# set 0 to disable limit and file cache, default: 100 # set 0 to disable limit and file cache, default: 100
attr_accessor :cache_limit attr_accessor :cache_limit
# Color style, default: :colorful, allows: [:colorful, :black_white]
attr_accessor :style
# session[:_rucaptcha] expire time, default 2 minutes # session[:_rucaptcha] expire time, default 2 minutes
attr_accessor :expires_in attr_accessor :expires_in
end end

View File

@ -1,3 +1,3 @@
module RuCaptcha module RuCaptcha
VERSION = '0.3.3' VERSION = '0.4.0'
end end

View File

@ -16,4 +16,22 @@ describe RuCaptcha::Captcha do
end end
end 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 end