ruby_color_contrast_checker 0.2.0 → 0.3.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/Gemfile.lock +1 -1
- data/README.md +4 -5
- data/lib/ruby_color_contrast_checker/version.rb +1 -1
- data/lib/ruby_color_contrast_checker.rb +81 -44
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 41127405d0f39eaf9fa5f9ac4743232a3644fe4a0ce8cda366762fc74c4c4a28
|
|
4
|
+
data.tar.gz: d48a9034eb3f53f6fbbf8123c777a0ec53a75037fe8fe82942b9dc228d0cfa64
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c5c5cf0bc32f2f378a929e298feedcf589efd4bf2d360b84040947093e8543c35dbb721087eb8192c5fd9607330a56988aafcef223e04f6f46019fdc4047a3bf
|
|
7
|
+
data.tar.gz: d75486f0d90127c0031e9fb8f5575b80a22cd242f30a26f6184c63af7e7f3f6ef0b0599d786da506de5edadaf444e45d84056f13977bc41379cf35fa88f80355
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -47,10 +47,10 @@ Further information can be found [here](https://webaim.org/articles/contrast/).
|
|
|
47
47
|
|
|
48
48
|
In summary:
|
|
49
49
|
|
|
50
|
-
- Level AA : Ratio
|
|
51
|
-
- Level AA (Large) : Ratio
|
|
52
|
-
- Level AAA : Ratio
|
|
53
|
-
- Level AAA (Large) : Ratio
|
|
50
|
+
- Level AA : Ratio >= 4.5
|
|
51
|
+
- Level AA (Large) : Ratio >= 3.0
|
|
52
|
+
- Level AAA : Ratio >= 7.0
|
|
53
|
+
- Level AAA (Large) : Ratio >= 4.5
|
|
54
54
|
|
|
55
55
|
Note:
|
|
56
56
|
|
|
@@ -133,5 +133,4 @@ Everyone interacting in the Ruby Color Contrast Checker project's codebases, iss
|
|
|
133
133
|
|
|
134
134
|
## 🌟 Special Thanks
|
|
135
135
|
|
|
136
|
-
- [WebAIM Contrast Checker API](https://webaim.org/resources/contrastchecker/) for the contrast checker API.
|
|
137
136
|
- [Richard Bates](https://github.com/richo225) for the [blog post](https://richardbates.dev/blog/2023-05-05) on releasing a gem.
|
|
@@ -1,27 +1,36 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative "ruby_color_contrast_checker/version"
|
|
4
|
-
require "uri"
|
|
5
|
-
require "net/http"
|
|
6
|
-
require "json"
|
|
7
4
|
|
|
8
5
|
module RubyColorContrastChecker
|
|
9
6
|
def self.run
|
|
10
7
|
Class.new { extend RubyColorContrastChecker }.run
|
|
11
8
|
end
|
|
12
9
|
|
|
13
|
-
def
|
|
14
|
-
|
|
15
|
-
end
|
|
10
|
+
def run
|
|
11
|
+
print_welcome_message
|
|
16
12
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
13
|
+
loop do
|
|
14
|
+
puts
|
|
15
|
+
first = prompt_input("\e[36mEnter the first hex color string:\e[0m \n\e[30m> #\e[0m")
|
|
16
|
+
second = prompt_input("\e[36mEnter the second hex color string:\e[0m \n\e[30m> #\e[0m")
|
|
17
|
+
puts
|
|
20
18
|
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
if !valid_hex?(first) || !valid_hex?(second)
|
|
20
|
+
print_error_message
|
|
21
|
+
else
|
|
22
|
+
data = get_result(first, second)
|
|
23
|
+
print_data(data)
|
|
24
|
+
end
|
|
23
25
|
|
|
24
|
-
|
|
26
|
+
puts
|
|
27
|
+
input = prompt_input("\e[33mContinue?\e[0m (\e[32myes\e[0m / \e[31mno\e[0m) ")
|
|
28
|
+
break if input.downcase == "no"
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def valid_hex?(hex)
|
|
33
|
+
!!(hex =~ /^[a-f0-9]{6}$/i) || !!(hex =~ /^[a-f0-9]{3}$/i)
|
|
25
34
|
end
|
|
26
35
|
|
|
27
36
|
def convert_3hex_to_6hex(hex)
|
|
@@ -29,31 +38,57 @@ module RubyColorContrastChecker
|
|
|
29
38
|
"#{chars[0]}#{chars[0]}#{chars[1]}#{chars[1]}#{chars[2]}#{chars[2]}"
|
|
30
39
|
end
|
|
31
40
|
|
|
32
|
-
def
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
end
|
|
41
|
+
def calculate_contrast_ratio(hex1, hex2)
|
|
42
|
+
rgb1 = convert_6hex_to_rgb_hash(hex1)
|
|
43
|
+
rgb2 = convert_6hex_to_rgb_hash(hex2)
|
|
36
44
|
|
|
37
|
-
|
|
38
|
-
|
|
45
|
+
luminance1 = calculate_luminance(rgb1)
|
|
46
|
+
luminance2 = calculate_luminance(rgb2)
|
|
47
|
+
|
|
48
|
+
((luminance1 > luminance2) ? ((luminance1 + 0.05) / (luminance2 + 0.05)) : ((luminance2 + 0.05) / (luminance1 + 0.05))).round(2)
|
|
39
49
|
end
|
|
40
50
|
|
|
41
|
-
|
|
42
|
-
|
|
51
|
+
# Helper methods to calculate contrast ratio
|
|
52
|
+
def convert_6hex_to_rgb_hash(hex)
|
|
53
|
+
value = Integer(hex, 16)
|
|
54
|
+
r = (value >> 16) & 255
|
|
55
|
+
g = (value >> 8) & 255
|
|
56
|
+
b = value & 255
|
|
57
|
+
|
|
58
|
+
{r:, g:, b:}
|
|
43
59
|
end
|
|
44
60
|
|
|
45
|
-
def
|
|
46
|
-
|
|
61
|
+
def calculate_luminance(rgb)
|
|
62
|
+
rgb.each do |key, value|
|
|
63
|
+
value /= 255.0
|
|
64
|
+
rgb[key] = ((value <= 0.03928) ? value / 12.92 : ((value + 0.055) / 1.055)**2.4)
|
|
65
|
+
end
|
|
47
66
|
|
|
48
|
-
|
|
67
|
+
(rgb[:r] * 0.2126 + rgb[:g] * 0.7152 + rgb[:b] * 0.0722).round(4)
|
|
49
68
|
end
|
|
50
69
|
|
|
51
|
-
def
|
|
52
|
-
|
|
70
|
+
def pass_or_fail(value, threshold)
|
|
71
|
+
(value >= threshold) ? "PASS" : "FAIL"
|
|
72
|
+
end
|
|
53
73
|
|
|
54
|
-
|
|
74
|
+
def get_result(hex1, hex2)
|
|
75
|
+
hex1 = convert_3hex_to_6hex(hex1) if hex1.length == 3
|
|
76
|
+
hex2 = convert_3hex_to_6hex(hex2) if hex2.length == 3
|
|
55
77
|
|
|
56
|
-
|
|
78
|
+
contrast_ratio = calculate_contrast_ratio(hex1, hex2)
|
|
79
|
+
|
|
80
|
+
{
|
|
81
|
+
"ratio" => contrast_ratio.to_s,
|
|
82
|
+
"AA" => pass_or_fail(contrast_ratio, 4.5),
|
|
83
|
+
"AALarge" => pass_or_fail(contrast_ratio, 3.0),
|
|
84
|
+
"AAA" => pass_or_fail(contrast_ratio, 7.0),
|
|
85
|
+
"AAALarge" => pass_or_fail(contrast_ratio, 4.5)
|
|
86
|
+
}
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def prompt_input(message)
|
|
90
|
+
print message
|
|
91
|
+
gets.chomp
|
|
57
92
|
end
|
|
58
93
|
|
|
59
94
|
def print_data(data)
|
|
@@ -88,25 +123,27 @@ module RubyColorContrastChecker
|
|
|
88
123
|
puts "\e[31mThe Hex color code is invalid.\e[0m"
|
|
89
124
|
end
|
|
90
125
|
|
|
91
|
-
|
|
92
|
-
|
|
126
|
+
# Helper methods to colorize string based on the conditions.
|
|
127
|
+
def colorize_float(float_string)
|
|
128
|
+
return red(float_string) if Float(float_string) < 4.5
|
|
93
129
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
first = prompt_input("\e[36mEnter the first hex color string:\e[0m \n\e[30m> #\e[0m")
|
|
97
|
-
second = prompt_input("\e[36mEnter the second hex color string:\e[0m \n\e[30m> #\e[0m")
|
|
98
|
-
puts
|
|
130
|
+
green(float_string)
|
|
131
|
+
end
|
|
99
132
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
else
|
|
103
|
-
data = fetch_data(first, second)
|
|
104
|
-
print_data(data)
|
|
105
|
-
end
|
|
133
|
+
def colorize_status(status_string)
|
|
134
|
+
status_string = status_string.upcase
|
|
106
135
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
136
|
+
return red(status_string) if status_string == "FAIL"
|
|
137
|
+
|
|
138
|
+
green(status_string)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# Helper methods to colorize string.
|
|
142
|
+
def red(string)
|
|
143
|
+
"\e[31m#{string}\e[0m"
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def green(string)
|
|
147
|
+
"\e[32m#{string}\e[0m"
|
|
111
148
|
end
|
|
112
149
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ruby_color_contrast_checker
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Chee Hwa Tang
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2023-05-
|
|
11
|
+
date: 2023-05-31 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: 'CLI interface for WCAG Color Contrast Checking in Ruby '
|
|
14
14
|
email:
|