chroma_wcag_contrast 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f1e8948f46aa31b1118dab3f889c231c25421cf1
4
+ data.tar.gz: ca99c1d5381c5fdded2ad68f618ebdf3634667c8
5
+ SHA512:
6
+ metadata.gz: f427a4506eed4984873b1caf7305521db989e22ec0d80b145f7445a985c1666cd01a4438965bf71c142d518073627727a53836fd0fa4f014f4f39eafbb475854
7
+ data.tar.gz: dce5fde6950a76ec12f60c2e5ea47639c185ef58ea689a6394b678714ee0c39e7432bffe6fee8bd49273d2439f7770dcfdcb89ef5a39e0ef021168fa13fd0149
@@ -0,0 +1,6 @@
1
+ .bundle/
2
+ log/*.log
3
+ tmp/*
4
+ coverage/*
5
+ *.gem
6
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
@@ -0,0 +1,17 @@
1
+ ChromaWcagContrast
2
+ ---
3
+
4
+ Calculates the WCAG contrast ratio between two `Chroma::Color`s. If you need this ratio, and you also use [chroma](https://github.com/jfairbank/chroma), then this library may be for you. Otherwise, you might prefer https://github.com/mkdynamic/wcag_color_contrast.
5
+
6
+ Available on Rubygems as `chroma_wcag_contrast`.
7
+
8
+ ```rb
9
+ ChromaWcagContrast.ratio(
10
+ Chroma.paint('999'),
11
+ Chroma.paint('ffffff')
12
+ ) # => 2.849
13
+ ```
14
+
15
+ ### License
16
+
17
+ MIT
@@ -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,21 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ # Maintain your gem's version:
4
+ require 'chroma_wcag_contrast/version'
5
+
6
+ # Describe your gem and declare its dependencies:
7
+ Gem::Specification.new do |s|
8
+ s.name = 'chroma_wcag_contrast'
9
+ s.version = ChromaWcagContrast::VERSION
10
+ s.required_ruby_version = Gem::Requirement.new('>= 2.0.0')
11
+ s.authors = ['Adam Becker']
12
+ s.summary = 'Calculate the contrast ratio between 2 colors. Depends on the chroma library.'
13
+ s.email = 'adam@dobt.co'
14
+ s.license = 'MIT'
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {features,spec}/*`.split("\n")
17
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ s.homepage = 'https://github.com/ajb/chroma_wcag_contrast'
19
+
20
+ s.add_dependency 'chroma'
21
+ end
@@ -0,0 +1,35 @@
1
+ module ChromaWcagContrast
2
+ class << self
3
+ # http://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef
4
+ # @param color1 [Chroma::Color]
5
+ # @param color2 [Chroma::Color]
6
+ # @return [Float]
7
+ def ratio(color1, color2)
8
+ high = luminance(color1)
9
+ low = luminance(color2)
10
+
11
+ if low > high
12
+ high, low = low, high
13
+ end
14
+
15
+ (high + 0.05) / (low + 0.05)
16
+ end
17
+
18
+ private
19
+
20
+ # http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
21
+ # @param color [Chroma::Color]
22
+ # @return Float
23
+ def luminance(color)
24
+ new_vals = color.rgb.to_a[0..2].map { |x| x.to_f / 255 }.map do |x|
25
+ if x < 0.03928
26
+ x / 12.92
27
+ else
28
+ ((x + 0.055) / 1.055)**2.4
29
+ end
30
+ end
31
+
32
+ (0.2126 * new_vals[0]) + (0.7152 * new_vals[1]) + (0.0722 * new_vals[2])
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module ChromaWcagContrast
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,12 @@
1
+ require 'bundler/setup'
2
+ require 'minitest/autorun'
3
+ require 'chroma'
4
+ require 'chroma_wcag_contrast'
5
+
6
+ class WCAGColorContrastTest < MiniTest::Unit::TestCase
7
+ def test_ratios
8
+ assert_in_delta 2.849, ChromaWcagContrast.ratio(Chroma.paint('999'), Chroma.paint('ffffff'))
9
+ assert_in_delta 1.425, ChromaWcagContrast.ratio(Chroma.paint('d8d8d8'), Chroma.paint('fff'))
10
+ assert_in_delta 1.956, ChromaWcagContrast.ratio(Chroma.paint('eee'), Chroma.paint('AAABBB'))
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chroma_wcag_contrast
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Adam Becker
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: chroma
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email: adam@dobt.co
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - ".gitignore"
34
+ - Gemfile
35
+ - README.md
36
+ - Rakefile
37
+ - chroma_wcag_contrast.gemspec
38
+ - lib/chroma_wcag_contrast.rb
39
+ - lib/chroma_wcag_contrast/version.rb
40
+ - test/test.rb
41
+ homepage: https://github.com/ajb/chroma_wcag_contrast
42
+ licenses:
43
+ - MIT
44
+ metadata: {}
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 2.0.0
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 2.2.2
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: Calculate the contrast ratio between 2 colors. Depends on the chroma library.
65
+ test_files: []
66
+ has_rdoc: