wcag_color_contrast 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in wcag_color_contrast.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Mark Dodwell
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,43 @@
1
+ # WCAGColorContrast
2
+
3
+ A Ruby port of the Javascript https://github.com/doochik/wcag-color-contrast.
4
+
5
+ Calculates the contrast ratio between 2 colors, for checking against the WCAG recommended contrast ratio for legibility.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'wcag_color_contrast'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ ```bash
18
+ $ bundle
19
+ ```
20
+
21
+ Or install it yourself as:
22
+
23
+ ```bash
24
+ $ gem install wcag_color_contrast
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ Pass 2 rgb colors are strings (3 or 6 characters, case insensitive):
30
+
31
+ ```ruby
32
+ require 'wcag_color_contrast'
33
+ WCAGColorContrast.new.ratio('999', 'ffffff')
34
+ #=> 2.849027755287037
35
+ ```
36
+
37
+ ## Contributing
38
+
39
+ 1. Fork it
40
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
41
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
42
+ 4. Push to the branch (`git push origin my-new-feature`)
43
+ 5. Create new Pull Request
@@ -0,0 +1,8 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |task|
5
+ task.libs << 'test'
6
+ end
7
+
8
+ task :default => :test
@@ -0,0 +1,51 @@
1
+ require "wcag_color_contrast/version"
2
+
3
+ module WCAGColorContrast
4
+ class InvalidColorError < StandardError; end
5
+
6
+ # Helper method for WCAGColorContrast#new#ratio.
7
+ def self.ratio(*args)
8
+ Ratio.new.ratio(*args)
9
+ end
10
+
11
+ class Ratio
12
+ # Calculate contast ratio beetween RGB1 and RGB2.
13
+ def ratio(rgb1, rgb2)
14
+ raise InvalidColorError, rgb1 unless valid_rgb?(rgb1)
15
+ raise InvalidColorError, rgb2 unless valid_rgb?(rgb2)
16
+
17
+ srgb1 = rgb_to_srgba(rgb1)
18
+ srgb2 = rgb_to_srgba(rgb2)
19
+
20
+ l1 = srgb_lightness(srgb1)
21
+ l2 = srgb_lightness(srgb2)
22
+
23
+ l1 > l2 ? (l1 + 0.05) / (l2 + 0.05) : (l2 + 0.05) / (l1 + 0.05)
24
+ end
25
+
26
+ private
27
+
28
+ # Convert RGB color to sRGB.
29
+ def rgb_to_srgba(rgb)
30
+ rgb << rgb if rgb.size == 3
31
+ [
32
+ rgb.slice(0,2).to_i(16) / 255.0,
33
+ rgb.slice(2,2).to_i(16) / 255.0,
34
+ rgb.slice(4,2).to_i(16) / 255.0
35
+ ]
36
+ end
37
+
38
+ # Calculate lightness for sRGB color.
39
+ def srgb_lightness(srgb)
40
+ r, g, b = srgb
41
+ 0.2126 * (r <= 0.03928 ? r / 12.92 : ((r + 0.055) / 1.055) ** 2.4) +
42
+ 0.7152 * (g <= 0.03928 ? g / 12.92 : ((g + 0.055) / 1.055) ** 2.4) +
43
+ 0.0722 * (b <= 0.03928 ? b / 12.92 : ((b + 0.055) / 1.055) ** 2.4)
44
+ end
45
+
46
+ # Validate RGB string.
47
+ def valid_rgb?(rgb)
48
+ rgb && (rgb.match(/^[a-f0-9]{3}$/i) || rgb.match(/^[a-f0-9]{6}$/i))
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module WCAGColorContrast
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,11 @@
1
+ require 'bundler/setup'
2
+ require 'minitest/autorun'
3
+ require 'wcag_color_contrast'
4
+
5
+ class WCAGColorContrastTest < MiniTest::Unit::TestCase
6
+ def test_ratios
7
+ assert_in_delta 2.849, WCAGColorContrast.ratio('999', 'ffffff')
8
+ assert_in_delta 1.425, WCAGColorContrast.ratio('d8d8d8', 'fff')
9
+ assert_in_delta 1.956, WCAGColorContrast.ratio('eee', 'AAABBB')
10
+ end
11
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'wcag_color_contrast/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'wcag_color_contrast'
8
+ spec.version = WCAGColorContrast::VERSION
9
+ spec.authors = ['Mark Dodwell']
10
+ spec.email = ['mark@mkdynamic.co.uk']
11
+ spec.summary = 'Calculate the contrast ratio between 2 colors, for checking against the WCAG recommended contrast ratio for legibility.'
12
+ spec.homepage = 'https://github.com/mkdynamic/wcag_color_contrast'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.test_files = `git ls-files -- {test,spec,features}/*`.split($/)
17
+ spec.executables = `git ls-files -- bin/*`.split($/).map &File.method(:basename)
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_development_dependency 'bundler', '~> 1.3'
21
+ spec.add_development_dependency 'rake'
22
+ spec.add_development_dependency 'minitest'
23
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wcag_color_contrast
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mark Dodwell
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: minitest
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description:
63
+ email:
64
+ - mark@mkdynamic.co.uk
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - lib/wcag_color_contrast.rb
75
+ - lib/wcag_color_contrast/version.rb
76
+ - test/test.rb
77
+ - wcag_color_contrast.gemspec
78
+ homepage: https://github.com/mkdynamic/wcag_color_contrast
79
+ licenses:
80
+ - MIT
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ segments:
92
+ - 0
93
+ hash: -3364554805105049379
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ segments:
101
+ - 0
102
+ hash: -3364554805105049379
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 1.8.25
106
+ signing_key:
107
+ specification_version: 3
108
+ summary: Calculate the contrast ratio between 2 colors, for checking against the WCAG
109
+ recommended contrast ratio for legibility.
110
+ test_files:
111
+ - test/test.rb