css_color_contrast 0.1.0 → 0.2.0
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/.rubocop.yml +4 -0
- data/.travis.yml +15 -7
- data/Gemfile.lock +35 -30
- data/README.md +77 -0
- data/css_color_contrast.gemspec +4 -3
- data/exe/css_color_contrast +5 -1
- data/lib/css_color_contrast.rb +47 -1
- data/lib/css_color_contrast/cli.rb +42 -0
- data/lib/css_color_contrast/color.rb +8 -0
- data/lib/css_color_contrast/command_interpreter.rb +192 -0
- data/lib/css_color_contrast/version.rb +1 -1
- metadata +14 -9
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2c0c2ebbd18a381d1ec4d2ac300a727539a019e8fb7cdea9361a293a15ed79f1
|
|
4
|
+
data.tar.gz: 27a1a2a1219fd0b2badfcc2c294ff2b1a9674010983716626e5db74318645c02
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a1236571f2bdad8aefc191a76870ccfec244cb63bbf5ee47050ecc9c31a63efe17fe75f17989f078f9732bb21aeb7493020b9846ab2be1cbb7a8fdcc06f3e494
|
|
7
|
+
data.tar.gz: 99858a144c1ca5bccc9d363c3f0d393ab392f4c215a37b7bd97f5abab3fac4fc70e475305cc2910d794ba545845e4e737a3d1b555a628c918012d957a8f44582
|
data/.rubocop.yml
ADDED
data/.travis.yml
CHANGED
|
@@ -1,12 +1,20 @@
|
|
|
1
1
|
---
|
|
2
2
|
language: ruby
|
|
3
3
|
cache: bundler
|
|
4
|
+
install: bundle install --jobs=3 --retry=3
|
|
4
5
|
rvm:
|
|
5
|
-
- 2.
|
|
6
|
-
- 2.
|
|
7
|
-
- 2.
|
|
8
|
-
-
|
|
9
|
-
- 2.7.0
|
|
6
|
+
- 2.5
|
|
7
|
+
- 2.6
|
|
8
|
+
- 2.7
|
|
9
|
+
- 3.0
|
|
10
10
|
- ruby-head
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
before_install: gem install bundler -v '~>2.0'
|
|
12
|
+
jobs:
|
|
13
|
+
include:
|
|
14
|
+
- rvm: 2.4
|
|
15
|
+
before_install:
|
|
16
|
+
- yes | gem update --system --force
|
|
17
|
+
- gem install bundler
|
|
18
|
+
- rvm: jruby-head
|
|
19
|
+
before_install:
|
|
20
|
+
- bundle add rake --version "13.0.0"
|
data/Gemfile.lock
CHANGED
|
@@ -2,43 +2,48 @@ PATH
|
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
4
|
css_color_contrast (0.1.0)
|
|
5
|
-
color_contrast_calc (~> 0.
|
|
5
|
+
color_contrast_calc (~> 0.9)
|
|
6
6
|
|
|
7
7
|
GEM
|
|
8
8
|
remote: https://rubygems.org/
|
|
9
9
|
specs:
|
|
10
|
-
ast (2.4.
|
|
11
|
-
color_contrast_calc (0.
|
|
12
|
-
diff-lcs (1.
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
ast (~> 2.4.0)
|
|
10
|
+
ast (2.4.1)
|
|
11
|
+
color_contrast_calc (0.9.0)
|
|
12
|
+
diff-lcs (1.4.4)
|
|
13
|
+
parallel (1.20.1)
|
|
14
|
+
parser (3.0.0.0)
|
|
15
|
+
ast (~> 2.4.1)
|
|
17
16
|
rainbow (3.0.0)
|
|
18
|
-
rake (13.0.
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
rspec-
|
|
23
|
-
|
|
24
|
-
rspec-
|
|
25
|
-
rspec-
|
|
17
|
+
rake (13.0.3)
|
|
18
|
+
regexp_parser (2.0.3)
|
|
19
|
+
rexml (3.2.4)
|
|
20
|
+
rspec (3.10.0)
|
|
21
|
+
rspec-core (~> 3.10.0)
|
|
22
|
+
rspec-expectations (~> 3.10.0)
|
|
23
|
+
rspec-mocks (~> 3.10.0)
|
|
24
|
+
rspec-core (3.10.1)
|
|
25
|
+
rspec-support (~> 3.10.0)
|
|
26
|
+
rspec-expectations (3.10.1)
|
|
26
27
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
27
|
-
rspec-support (~> 3.
|
|
28
|
-
rspec-mocks (3.
|
|
28
|
+
rspec-support (~> 3.10.0)
|
|
29
|
+
rspec-mocks (3.10.1)
|
|
29
30
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
30
|
-
rspec-support (~> 3.
|
|
31
|
-
rspec-support (3.
|
|
32
|
-
rubocop (
|
|
33
|
-
jaro_winkler (~> 1.5.1)
|
|
31
|
+
rspec-support (~> 3.10.0)
|
|
32
|
+
rspec-support (3.10.1)
|
|
33
|
+
rubocop (1.8.0)
|
|
34
34
|
parallel (~> 1.10)
|
|
35
|
-
parser (>=
|
|
35
|
+
parser (>= 3.0.0.0)
|
|
36
36
|
rainbow (>= 2.2.2, < 4.0)
|
|
37
|
+
regexp_parser (>= 1.8, < 3.0)
|
|
38
|
+
rexml
|
|
39
|
+
rubocop-ast (>= 1.2.0, < 2.0)
|
|
37
40
|
ruby-progressbar (~> 1.7)
|
|
38
|
-
unicode-display_width (>= 1.4.0, <
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
41
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
|
42
|
+
rubocop-ast (1.4.0)
|
|
43
|
+
parser (>= 2.7.1.5)
|
|
44
|
+
ruby-progressbar (1.11.0)
|
|
45
|
+
unicode-display_width (2.0.0)
|
|
46
|
+
yard (0.9.26)
|
|
42
47
|
|
|
43
48
|
PLATFORMS
|
|
44
49
|
ruby
|
|
@@ -47,9 +52,9 @@ DEPENDENCIES
|
|
|
47
52
|
bundler (~> 2.1)
|
|
48
53
|
css_color_contrast!
|
|
49
54
|
rake (~> 13.0)
|
|
50
|
-
rspec (~> 3.
|
|
51
|
-
rubocop (~>
|
|
55
|
+
rspec (~> 3.10)
|
|
56
|
+
rubocop (~> 1.8)
|
|
52
57
|
yard (~> 0.9)
|
|
53
58
|
|
|
54
59
|
BUNDLED WITH
|
|
55
|
-
2.1.
|
|
60
|
+
2.1.4
|
data/README.md
CHANGED
|
@@ -59,6 +59,83 @@ CssColorContrast.relative_luminance('#ff0')
|
|
|
59
59
|
# => 0.9278
|
|
60
60
|
```
|
|
61
61
|
|
|
62
|
+
## Command-line interface
|
|
63
|
+
|
|
64
|
+
A command-line tool, ```css_color_contrast``` is provided for a demonstration purpose.
|
|
65
|
+
|
|
66
|
+
Invoke it as follows:
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
$ css_color_contrast
|
|
70
|
+
To calculate the contrast ratio between 2 colors, enter the following command:
|
|
71
|
+
|
|
72
|
+
> ratio: color1 color2
|
|
73
|
+
|
|
74
|
+
For the values of color1 and color2, hexadecimal notation, RGB/HSL/HWB
|
|
75
|
+
functions and the extended color keywords are supported.
|
|
76
|
+
|
|
77
|
+
To finish the program, enter 'exit'.
|
|
78
|
+
>
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Commands
|
|
82
|
+
|
|
83
|
+
#### ratio:
|
|
84
|
+
|
|
85
|
+
Calculate the contrast ratio between two colors as in the next case:
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
> ratio: rgb(255 255 0) black
|
|
89
|
+
19.555999999999997
|
|
90
|
+
>
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
#### adjust:
|
|
94
|
+
|
|
95
|
+
It takes 3 arguments, 2 colors and a contrast ratio to be satisfied.
|
|
96
|
+
|
|
97
|
+
In the example below, it tries to adjust the lightness of the second color so
|
|
98
|
+
that the contrast ratio between the 2 colors is just above the target ratio.
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
> adjust: white red 4.5
|
|
102
|
+
#ee0000
|
|
103
|
+
>
|
|
104
|
+
```
|
|
105
|
+
#### info:
|
|
106
|
+
|
|
107
|
+
Print the properties of a given color:
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
> info: #ff0
|
|
111
|
+
---
|
|
112
|
+
name: yellow
|
|
113
|
+
hex: #ffff00
|
|
114
|
+
rgb: rgb(255,255,0)
|
|
115
|
+
hsl: hsl(60.00,100.00%,50.00%)
|
|
116
|
+
hwb: hwb(60.00,0.00%,0.00%)
|
|
117
|
+
>
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
When the 3rd argument is omitted, it defaults to 4.5.
|
|
121
|
+
|
|
122
|
+
#### Assignment to a variable
|
|
123
|
+
|
|
124
|
+
You can assign a color value to a varible. Variable names begin with @.
|
|
125
|
+
|
|
126
|
+
```
|
|
127
|
+
> @deeper_red: #e00
|
|
128
|
+
#e00
|
|
129
|
+
> ratio: white @deeper_red
|
|
130
|
+
4.530325445433122
|
|
131
|
+
> info: @deeper_red
|
|
132
|
+
---
|
|
133
|
+
name: #ee0000
|
|
134
|
+
hex: #ee0000
|
|
135
|
+
rgb: rgb(238,0,0)
|
|
136
|
+
hsl: hsl(0.00,100.00%,46.67%)
|
|
137
|
+
hwb: hwb(0.00,0.00%,6.67%)
|
|
138
|
+
```
|
|
62
139
|
## Development
|
|
63
140
|
|
|
64
141
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/css_color_contrast.gemspec
CHANGED
|
@@ -10,6 +10,7 @@ Gem::Specification.new do |spec|
|
|
|
10
10
|
Utility to calculate the contrast ratio between 2 colors, for checking
|
|
11
11
|
the level of conformance to the WCAG 2.0 criteria.
|
|
12
12
|
Hexadecimal notation, RGB/HSL/HWB functions are supported as input formats.
|
|
13
|
+
And a command-line interface is provided for a demonstration purpose.
|
|
13
14
|
SUMMARY
|
|
14
15
|
#spec.description = %q{TODO: Write a longer description or delete this line.}
|
|
15
16
|
spec.homepage = "https://github.com/nico-hn/css_color_contrast/"
|
|
@@ -31,11 +32,11 @@ Gem::Specification.new do |spec|
|
|
|
31
32
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
32
33
|
spec.require_paths = ["lib"]
|
|
33
34
|
|
|
34
|
-
spec.add_runtime_dependency "color_contrast_calc", "~> 0.
|
|
35
|
+
spec.add_runtime_dependency "color_contrast_calc", "~> 0.9"
|
|
35
36
|
|
|
36
37
|
spec.add_development_dependency 'bundler', '~> 2.1'
|
|
37
38
|
spec.add_development_dependency 'rake', '~> 13.0'
|
|
38
|
-
spec.add_development_dependency 'rspec', '~> 3.
|
|
39
|
-
spec.add_development_dependency 'rubocop', '~>
|
|
39
|
+
spec.add_development_dependency 'rspec', '~> 3.10'
|
|
40
|
+
spec.add_development_dependency 'rubocop', '~> 1.8'
|
|
40
41
|
spec.add_development_dependency 'yard', '~> 0.9'
|
|
41
42
|
end
|
data/exe/css_color_contrast
CHANGED
data/lib/css_color_contrast.rb
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require 'css_color_contrast/version'
|
|
4
4
|
require 'color_contrast_calc'
|
|
5
|
+
require 'css_color_contrast/color'
|
|
5
6
|
|
|
6
7
|
##
|
|
7
8
|
# Provide methods to calculate the contrast ratio
|
|
@@ -27,6 +28,27 @@ module CssColorContrast
|
|
|
27
28
|
ColorContrastCalc.contrast_ratio(color1, color2)
|
|
28
29
|
end
|
|
29
30
|
|
|
31
|
+
##
|
|
32
|
+
# Calculate the contrast ratio between two transparent colors.
|
|
33
|
+
#
|
|
34
|
+
# For the calculation of contrast ratio between foreground and
|
|
35
|
+
# background colors, you need another color which is placed below
|
|
36
|
+
# the former two colors, because the third color filters through
|
|
37
|
+
# the overlaid colors.
|
|
38
|
+
#
|
|
39
|
+
# @param foreground [String, Array<Integer>, Color] The uppermost
|
|
40
|
+
# color such as "rgb(255, 255, 0, 0.5)" or "hsl(60 100% 50% / 50%)"
|
|
41
|
+
# @param background [String, Array<Integer>, Color] The color placed
|
|
42
|
+
# between the others
|
|
43
|
+
# @param base [String, Array<Integer>, Color] The color placed in
|
|
44
|
+
# the bottom. When the backgound is completely opaque, this color
|
|
45
|
+
# is ignored.
|
|
46
|
+
# @return [Float] Contrast ratio
|
|
47
|
+
|
|
48
|
+
def self.ratio_with_opacity(foreground, background, base = Color::WHITE)
|
|
49
|
+
ColorContrastCalc.contrast_ratio_with_opacity(foreground, background, base)
|
|
50
|
+
end
|
|
51
|
+
|
|
30
52
|
##
|
|
31
53
|
# Calculate the relative luminance of a color.
|
|
32
54
|
#
|
|
@@ -40,6 +62,30 @@ module CssColorContrast
|
|
|
40
62
|
# @return [Float] Relative luminance of the passed color.
|
|
41
63
|
|
|
42
64
|
def self.relative_luminance(color)
|
|
43
|
-
|
|
65
|
+
Color.as_color(color).relative_luminance
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
##
|
|
69
|
+
# Adjust the lightness of the second color to satisfy a specified
|
|
70
|
+
# level of contrast ratio.
|
|
71
|
+
#
|
|
72
|
+
# @param fixed_color [String, Array<Integer, Float>] The color that
|
|
73
|
+
# remains unchanged
|
|
74
|
+
# @param color_to_adjust [String, Array<Integer, Float>] The color of
|
|
75
|
+
# which the lightness is to be adjusted
|
|
76
|
+
# @param level [Integer, Float] The contrast ratio to be satisfied,
|
|
77
|
+
# such as 3.0, 4.5, 7.0
|
|
78
|
+
# @return [Color, nil] The color of which the contrast ratio against
|
|
79
|
+
# fixed_color equals or is slightly greater than the specified level.
|
|
80
|
+
|
|
81
|
+
def self.adjust_lightness(fixed_color, color_to_adjust, level = 4.5)
|
|
82
|
+
fixed, to_adjust = [fixed_color, color_to_adjust].map do |color|
|
|
83
|
+
Color.as_color(color)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
adjusted = fixed.find_lightness_threshold(to_adjust, level)
|
|
87
|
+
if_satisfied = fixed.contrast_ratio_against(adjusted) >= level
|
|
88
|
+
|
|
89
|
+
if_satisfied ? adjusted : nil
|
|
44
90
|
end
|
|
45
91
|
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'readline'
|
|
4
|
+
require 'css_color_contrast/command_interpreter'
|
|
5
|
+
|
|
6
|
+
module CssColorContrast
|
|
7
|
+
module Cli
|
|
8
|
+
BANNER = <<~BANNER
|
|
9
|
+
To calculate the contrast ratio between 2 colors, enter the following command:
|
|
10
|
+
|
|
11
|
+
> ratio: color1 color2
|
|
12
|
+
|
|
13
|
+
For the values of color1 and color2, hexadecimal notation, RGB/HSL/HWB
|
|
14
|
+
functions and the extended color keywords are supported.
|
|
15
|
+
|
|
16
|
+
To finish the program, enter 'exit'.
|
|
17
|
+
BANNER
|
|
18
|
+
|
|
19
|
+
def self.print_banner
|
|
20
|
+
$stdout.print BANNER
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def self.read_commands
|
|
24
|
+
env = {}
|
|
25
|
+
|
|
26
|
+
while line = Readline.readline('> ', true)
|
|
27
|
+
begin
|
|
28
|
+
exit if line.chomp == 'exit'
|
|
29
|
+
func = CommandInterpreter::Parser.parse!(line.chomp, env)
|
|
30
|
+
$stdout.puts func.evaluate
|
|
31
|
+
rescue => e
|
|
32
|
+
puts e
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def self.start
|
|
38
|
+
print_banner
|
|
39
|
+
read_commands
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'strscan'
|
|
4
|
+
require 'stringio'
|
|
5
|
+
require 'css_color_contrast'
|
|
6
|
+
|
|
7
|
+
module CssColorContrast
|
|
8
|
+
module CommandInterpreter
|
|
9
|
+
module TokenRe
|
|
10
|
+
FUNC_HEAD = /:\s*/.freeze
|
|
11
|
+
LABEL = /[^\s:()]+/.freeze
|
|
12
|
+
SPACE = /\s+/.freeze
|
|
13
|
+
COLOR_SCHEME = /(rgba?|hsl|hwb)/i.freeze
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
class Value
|
|
17
|
+
attr_reader :source
|
|
18
|
+
|
|
19
|
+
def self.assign(source, env, value = nil)
|
|
20
|
+
new(source, env, value)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def initialize(source, env, value = nil)
|
|
24
|
+
@source = source
|
|
25
|
+
@env = env
|
|
26
|
+
@value = value
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def evaluate
|
|
30
|
+
return @env[@source] if @source.start_with?('@')
|
|
31
|
+
|
|
32
|
+
@value || @source
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
class Function
|
|
37
|
+
attr_reader :name, :params
|
|
38
|
+
|
|
39
|
+
def initialize(name, env = {})
|
|
40
|
+
@name = name
|
|
41
|
+
@env = env
|
|
42
|
+
@params = []
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def push(param)
|
|
46
|
+
@params.push(param)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def param_values
|
|
50
|
+
@params.map(&:evaluate).flatten
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
class AssignVariable < self
|
|
54
|
+
def evaluate
|
|
55
|
+
@env[@name] = param_values
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
class Ratio < self
|
|
60
|
+
def evaluate
|
|
61
|
+
ColorContrastCalc.contrast_ratio(*param_values)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
class AdjustLightness < self
|
|
66
|
+
DEFAULT_RATIO = 4.5
|
|
67
|
+
def ratio_given(ratio)
|
|
68
|
+
ratio && /\A\d+(\.\d+)?/.match?(ratio)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def evaluate
|
|
72
|
+
fixed, to_adjust, ratio = param_values
|
|
73
|
+
|
|
74
|
+
ratio = ratio_given(ratio) ? ratio.to_f : DEFAULT_RATIO
|
|
75
|
+
|
|
76
|
+
CssColorContrast.adjust_lightness(fixed, to_adjust, ratio)
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
class Info < self
|
|
81
|
+
def evaluate
|
|
82
|
+
colors = param_values.map {|c| Color.as_color(c) }
|
|
83
|
+
out = StringIO.new
|
|
84
|
+
|
|
85
|
+
colors.each do |c|
|
|
86
|
+
out.puts '---'
|
|
87
|
+
out.puts labeled_rgb_values(c)
|
|
88
|
+
out.puts format_color_function(:hsl, c.hsl)
|
|
89
|
+
out.puts format_color_function(:hwb, c.hwb)
|
|
90
|
+
end
|
|
91
|
+
out.string
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def labeled_rgb_values(color)
|
|
95
|
+
%i[name hex rgb]
|
|
96
|
+
.zip([color.name, color.hex, color.to_s(10)])
|
|
97
|
+
.map {|v| v.join(': ') }
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
private :labeled_rgb_values
|
|
101
|
+
|
|
102
|
+
def format_color_function(scheme, components)
|
|
103
|
+
format("#{scheme}: #{scheme}(%3.2f,%3.2f%%,%3.2f%%)", *components)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
private :format_color_function
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
FUNCTION_TABLE = {
|
|
110
|
+
'ratio' => Ratio,
|
|
111
|
+
'adjust' => AdjustLightness,
|
|
112
|
+
'info' => Info
|
|
113
|
+
}.freeze
|
|
114
|
+
|
|
115
|
+
private_constant :FUNCTION_TABLE
|
|
116
|
+
|
|
117
|
+
def self.create(name, env = {})
|
|
118
|
+
return AssignVariable.new(name, env) if name.start_with?('@')
|
|
119
|
+
|
|
120
|
+
FUNCTION_TABLE[name].new(name, env)
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
class Parser
|
|
125
|
+
def self.parse!(line, env = {})
|
|
126
|
+
new(line, env).parse!.root_node
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
attr_reader :tokens, :node_tree
|
|
130
|
+
|
|
131
|
+
def initialize(line, env = {})
|
|
132
|
+
@scanner = StringScanner.new(line)
|
|
133
|
+
@env = env
|
|
134
|
+
@tokens = []
|
|
135
|
+
@node_tree = []
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def root_node
|
|
139
|
+
@node_tree.first
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def current_node
|
|
143
|
+
@node_tree.last
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
private :current_node
|
|
147
|
+
|
|
148
|
+
def read_label
|
|
149
|
+
cur_pos = @scanner.pos
|
|
150
|
+
label = @scanner.scan(TokenRe::LABEL)
|
|
151
|
+
return unless label
|
|
152
|
+
|
|
153
|
+
if TokenRe::COLOR_SCHEME.match?(label)
|
|
154
|
+
@tokens.push read_color_function(cur_pos)
|
|
155
|
+
else
|
|
156
|
+
@tokens.push label
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def read_color_function(cur_pos)
|
|
161
|
+
@scanner.pos = cur_pos
|
|
162
|
+
color = Color.as_color(@scanner.scan_until(/\)/))
|
|
163
|
+
source = @scanner.string[cur_pos..(@scanner.pos)]
|
|
164
|
+
Value.assign(source, @env, color)
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def read_separator
|
|
168
|
+
if @scanner.scan(TokenRe::FUNC_HEAD)
|
|
169
|
+
@node_tree.push(Function.create(@tokens.pop, @env))
|
|
170
|
+
elsif @scanner.scan(TokenRe::SPACE) || @scanner.eos?
|
|
171
|
+
token = @tokens.pop
|
|
172
|
+
value = token.is_a?(Value) ? token : Value.assign(token, @env)
|
|
173
|
+
current_node.push(value)
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
read_label
|
|
177
|
+
read_separator unless @tokens.empty?
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
def ignore_space
|
|
181
|
+
@scanner.scan(TokenRe::SPACE)
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def parse!
|
|
185
|
+
ignore_space
|
|
186
|
+
read_label
|
|
187
|
+
read_separator
|
|
188
|
+
self
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: css_color_contrast
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- HASHIMOTO, Naoki
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2021-01-11 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: color_contrast_calc
|
|
@@ -16,14 +16,14 @@ dependencies:
|
|
|
16
16
|
requirements:
|
|
17
17
|
- - "~>"
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: 0.
|
|
19
|
+
version: '0.9'
|
|
20
20
|
type: :runtime
|
|
21
21
|
prerelease: false
|
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
23
|
requirements:
|
|
24
24
|
- - "~>"
|
|
25
25
|
- !ruby/object:Gem::Version
|
|
26
|
-
version: 0.
|
|
26
|
+
version: '0.9'
|
|
27
27
|
- !ruby/object:Gem::Dependency
|
|
28
28
|
name: bundler
|
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -58,28 +58,28 @@ dependencies:
|
|
|
58
58
|
requirements:
|
|
59
59
|
- - "~>"
|
|
60
60
|
- !ruby/object:Gem::Version
|
|
61
|
-
version: '3.
|
|
61
|
+
version: '3.10'
|
|
62
62
|
type: :development
|
|
63
63
|
prerelease: false
|
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
|
65
65
|
requirements:
|
|
66
66
|
- - "~>"
|
|
67
67
|
- !ruby/object:Gem::Version
|
|
68
|
-
version: '3.
|
|
68
|
+
version: '3.10'
|
|
69
69
|
- !ruby/object:Gem::Dependency
|
|
70
70
|
name: rubocop
|
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
|
72
72
|
requirements:
|
|
73
73
|
- - "~>"
|
|
74
74
|
- !ruby/object:Gem::Version
|
|
75
|
-
version: '
|
|
75
|
+
version: '1.8'
|
|
76
76
|
type: :development
|
|
77
77
|
prerelease: false
|
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
|
79
79
|
requirements:
|
|
80
80
|
- - "~>"
|
|
81
81
|
- !ruby/object:Gem::Version
|
|
82
|
-
version: '
|
|
82
|
+
version: '1.8'
|
|
83
83
|
- !ruby/object:Gem::Dependency
|
|
84
84
|
name: yard
|
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -104,6 +104,7 @@ extra_rdoc_files: []
|
|
|
104
104
|
files:
|
|
105
105
|
- ".gitignore"
|
|
106
106
|
- ".rspec"
|
|
107
|
+
- ".rubocop.yml"
|
|
107
108
|
- ".travis.yml"
|
|
108
109
|
- CODE_OF_CONDUCT.md
|
|
109
110
|
- Gemfile
|
|
@@ -116,6 +117,9 @@ files:
|
|
|
116
117
|
- css_color_contrast.gemspec
|
|
117
118
|
- exe/css_color_contrast
|
|
118
119
|
- lib/css_color_contrast.rb
|
|
120
|
+
- lib/css_color_contrast/cli.rb
|
|
121
|
+
- lib/css_color_contrast/color.rb
|
|
122
|
+
- lib/css_color_contrast/command_interpreter.rb
|
|
119
123
|
- lib/css_color_contrast/version.rb
|
|
120
124
|
homepage: https://github.com/nico-hn/css_color_contrast/
|
|
121
125
|
licenses:
|
|
@@ -143,5 +147,6 @@ signing_key:
|
|
|
143
147
|
specification_version: 4
|
|
144
148
|
summary: Utility to calculate the contrast ratio between 2 colors, for checking the
|
|
145
149
|
level of conformance to the WCAG 2.0 criteria. Hexadecimal notation, RGB/HSL/HWB
|
|
146
|
-
functions are supported as input formats.
|
|
150
|
+
functions are supported as input formats. And a command-line interface is provided
|
|
151
|
+
for a demonstration purpose.
|
|
147
152
|
test_files: []
|