luminosity_contrast 0.2.0 → 0.2.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.
- checksums.yaml +4 -4
- data/.gitignore +1 -1
- data/.yardopts +2 -0
- data/README.md +2 -0
- data/lib/luminosity_contrast.rb +22 -12
- data/lib/luminosity_contrast/color.rb +34 -4
- data/lib/luminosity_contrast/version.rb +2 -1
- data/luminosity_contrast.gemspec +2 -2
- metadata +4 -5
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4f3d154f90b97ea368aba6c9f1546e50c83ee878
|
|
4
|
+
data.tar.gz: 023801266540b5532fd9c9d5d70413e01b890197
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9bb5439bc49afe205a895732b9096f1fa6fb4189bfce4d915825fe4d6149652d5603197b2d60d6c304fe3431c76867f27a813f6a2c8bc7f1f621f067d1e7c84f
|
|
7
|
+
data.tar.gz: 5e0f9fac3a185deff38df95be40def5a66f45014adffa7d2a8f532c5b7680fd2b4611e1718c0fb49483e7847e9f6742df9f2831c7265fa7d59a9f9af634429c2
|
data/.gitignore
CHANGED
data/.yardopts
ADDED
data/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# LuminosityContrast
|
|
2
2
|
|
|
3
|
+
[](https://badge.fury.io/rb/luminosity_contrast)
|
|
4
|
+
|
|
3
5
|
Calculate the [Luminosity Contrast Ratio](https://www.w3.org/TR/WCAG20/#contrast-ratiodef) of two colors to determine whether text can be easily read. This quantifies the legibility of colored text on a colored background for accessibility purposes.
|
|
4
6
|
|
|
5
7
|
Implements the formula in WCAG 2.0 guideline 1.4
|
data/lib/luminosity_contrast.rb
CHANGED
|
@@ -1,34 +1,44 @@
|
|
|
1
1
|
require "luminosity_contrast/version"
|
|
2
2
|
require "luminosity_contrast/color"
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
# Namespace for the gem. Contains methods to implement the Luminosity Contrast
|
|
5
|
+
# Ratio formula.
|
|
6
|
+
#
|
|
7
|
+
# This module is not designed to be included in a class.
|
|
5
8
|
module LuminosityContrast
|
|
6
|
-
|
|
7
|
-
# module methods
|
|
8
9
|
class << self
|
|
9
|
-
#
|
|
10
|
-
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
#
|
|
10
|
+
# Calculate the Luminosity Contrast Ratio of two colors.
|
|
11
|
+
# This method is the gem's _raison d'être_.
|
|
12
|
+
#
|
|
13
|
+
# @param rgb1 either of two colors to compare
|
|
14
|
+
# @param rgb2 the other of two colors to compare
|
|
15
|
+
# @example two RGB values as Arrays of three numbers
|
|
16
|
+
# LuminosityContrast.ratio([0, 0, 0], [255.0, 255.0, 255.0])
|
|
17
|
+
# @example two hex code strings (3 or 6 characters)
|
|
18
|
+
# LuminosityContrast.ratio('000000', 'fff')
|
|
19
|
+
# @example two LuminosityContrast::Color objects
|
|
20
|
+
# LuminosityContrast.ratio(color1, color2)
|
|
21
|
+
# @return [Float] the Luminosity Contrast Ratio between 1.0 and 21.0
|
|
14
22
|
def ratio(rgb1, rgb2)
|
|
15
23
|
c1, c2 = [rgb1, rgb2].map { |rgb| Color.new(rgb) }
|
|
16
24
|
l1, l2 = [c1, c2].map(&:relative_luminance).sort
|
|
17
25
|
(l2 + 0.05) / (l1 + 0.05)
|
|
18
26
|
end
|
|
19
27
|
|
|
20
|
-
#
|
|
21
|
-
#
|
|
28
|
+
# @param r [Numeric] red component between 0.0 and 255.0
|
|
29
|
+
# @param g [Numeric] green component between 0.0 and 255.0
|
|
30
|
+
# @param b [Numeric] blue component between 0.0 and 255.0
|
|
31
|
+
# @return [Float] relative luminance of a color between 0.0 and 1.0
|
|
22
32
|
def relative_luminance(r, g, b)
|
|
23
33
|
(0.2126 * f(r)) + (0.7152 * f(g)) + (0.0722 * f(b))
|
|
24
34
|
end
|
|
25
35
|
|
|
26
36
|
# assists calculation of relative luminance
|
|
27
|
-
#
|
|
37
|
+
# @param a single RGB component between 0.0 and 255.0
|
|
38
|
+
# @returns [Float] a value needed for relative luminance calculation
|
|
28
39
|
private def f(component)
|
|
29
40
|
c = component / 255.0
|
|
30
41
|
c <= 0.03928 ? c : ((c + 0.055) / 1.055) ** 2.4
|
|
31
42
|
end
|
|
32
43
|
end
|
|
33
44
|
end
|
|
34
|
-
|
|
@@ -1,29 +1,59 @@
|
|
|
1
1
|
module LuminosityContrast
|
|
2
|
+
|
|
3
|
+
# @!attribute [r] r
|
|
4
|
+
# @return [Float] the red component
|
|
5
|
+
# @!attribute [r] g
|
|
6
|
+
# @return [Float] the green component
|
|
7
|
+
# @!attribute [r] b
|
|
8
|
+
# @return [Float] the blue component
|
|
9
|
+
#
|
|
10
|
+
# Color object parses RGB formats like hex code string or array of numbers.
|
|
11
|
+
# Populates RGB attributes and provides convenience methods for comparing
|
|
12
|
+
# colors. This class is used by LuminosityContrast.ratio but can also be used
|
|
13
|
+
# on its own.
|
|
2
14
|
class Color
|
|
15
|
+
|
|
3
16
|
attr_reader :r, :g, :b
|
|
4
17
|
|
|
18
|
+
# @param args a representation of an RGB color
|
|
19
|
+
# @see .rgb_from
|
|
5
20
|
def initialize(*args)
|
|
6
21
|
input = args.size == 1 ? args[0] : args
|
|
7
22
|
@r, @g, @b = Color.rgb_from(input)
|
|
8
23
|
end
|
|
9
24
|
|
|
25
|
+
# @return [Float] between 0.0 and 1.0
|
|
26
|
+
# @see LuminosityContrast.relative_luminance
|
|
10
27
|
def relative_luminance
|
|
11
28
|
LuminosityContrast.relative_luminance(r,g,b)
|
|
12
29
|
end
|
|
13
30
|
|
|
31
|
+
# @param color [Color] a second color contrasting with this one
|
|
32
|
+
# @return [Float] a ratio between 1.0 and 21.0
|
|
33
|
+
# @see LuminosityContrast.ratio
|
|
14
34
|
def ratio(color)
|
|
15
35
|
LuminosityContrast.ratio(self, color)
|
|
16
36
|
end
|
|
17
37
|
|
|
38
|
+
# @return [Array] red, green, blue color components from 0.0 to 255.0
|
|
18
39
|
def to_rgb
|
|
19
40
|
[r, g, b]
|
|
20
41
|
end
|
|
21
42
|
|
|
22
43
|
class << self
|
|
23
|
-
|
|
24
|
-
#
|
|
25
|
-
#
|
|
26
|
-
#
|
|
44
|
+
# Convert one of several RGB formats to an Array of components
|
|
45
|
+
# @return [Array] red, green, blue color components from 0.0 to 255.0
|
|
46
|
+
# @param input a representation of an RGB color
|
|
47
|
+
# @example 3 or 6 character hex color code String
|
|
48
|
+
# rgb_from('fff')
|
|
49
|
+
# rgb_from('000000')
|
|
50
|
+
# @example Array with 3 numbers between 0 and 255
|
|
51
|
+
# rgb_from([0, 127.0, 255])
|
|
52
|
+
# @example a Hash with 3 numbers between 0 and 255
|
|
53
|
+
# rgb_from(r: 0, g: 127.0, b: 255)
|
|
54
|
+
# @example an Object that responds to methods #r, #g, #b
|
|
55
|
+
# color = Color.new('000')
|
|
56
|
+
# rgb_from(color)
|
|
27
57
|
def rgb_from(input)
|
|
28
58
|
case input
|
|
29
59
|
when String # hex code
|
data/luminosity_contrast.gemspec
CHANGED
|
@@ -13,8 +13,8 @@ Gem::Specification.new do |spec|
|
|
|
13
13
|
spec.license = "MIT"
|
|
14
14
|
|
|
15
15
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
16
|
-
spec.bindir = "
|
|
17
|
-
spec.executables = spec.files.grep(%r{^
|
|
16
|
+
spec.bindir = "exe"
|
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
18
18
|
spec.require_paths = ["lib"]
|
|
19
19
|
|
|
20
20
|
spec.add_development_dependency "bundler", "~> 1.11"
|
metadata
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: luminosity_contrast
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.2.
|
|
4
|
+
version: 0.2.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Justin Jones
|
|
8
8
|
autorequire:
|
|
9
|
-
bindir:
|
|
9
|
+
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
11
|
date: 2016-04-27 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
@@ -54,14 +54,13 @@ dependencies:
|
|
|
54
54
|
version: '5.0'
|
|
55
55
|
description:
|
|
56
56
|
email:
|
|
57
|
-
executables:
|
|
58
|
-
- console
|
|
59
|
-
- setup
|
|
57
|
+
executables: []
|
|
60
58
|
extensions: []
|
|
61
59
|
extra_rdoc_files: []
|
|
62
60
|
files:
|
|
63
61
|
- ".gitignore"
|
|
64
62
|
- ".travis.yml"
|
|
63
|
+
- ".yardopts"
|
|
65
64
|
- CODE_OF_CONDUCT.md
|
|
66
65
|
- Gemfile
|
|
67
66
|
- LICENSE.txt
|