ruby_color_contrast_checker 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 40345e8b055888a53d71039c61513b0d6db1dd83acdf35657921bc9a303eed9f
4
- data.tar.gz: 47be0542a7799e2b788596e395ac989fddee8035bb66561c61cea513a298c157
3
+ metadata.gz: 41127405d0f39eaf9fa5f9ac4743232a3644fe4a0ce8cda366762fc74c4c4a28
4
+ data.tar.gz: d48a9034eb3f53f6fbbf8123c777a0ec53a75037fe8fe82942b9dc228d0cfa64
5
5
  SHA512:
6
- metadata.gz: 5fb1724ffe3dab3f991e1a896c83282b5cb997be99faecd2eaa27ec814f38417d75bd1166113e50333e351eaa76544bc315762dbacefbe588c3d55047150878f
7
- data.tar.gz: 1c340332128989b61e3a4460d052597664e8ec46389c5d259ee01c4e88e71435b5aa713a89dad184e49a36e5fae431937aa0786f6fd8619ae33fd990949e8cea
6
+ metadata.gz: c5c5cf0bc32f2f378a929e298feedcf589efd4bf2d360b84040947093e8543c35dbb721087eb8192c5fd9607330a56988aafcef223e04f6f46019fdc4047a3bf
7
+ data.tar.gz: d75486f0d90127c0031e9fb8f5575b80a22cd242f30a26f6184c63af7e7f3f6ef0b0599d786da506de5edadaf444e45d84056f13977bc41379cf35fa88f80355
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby_color_contrast_checker (0.2.0)
4
+ ruby_color_contrast_checker (0.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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 > 4.5
51
- - Level AA (Large) : Ratio > 3.0
52
- - Level AAA : Ratio > 7.0
53
- - Level AAA (Large) : Ratio > 4.5
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,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyColorContrastChecker
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
@@ -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 valid_hex?(hex)
14
- !!(hex =~ /^[a-f0-9]{6}$/i) || !!(hex =~ /^[a-f0-9]{3}$/i)
15
- end
10
+ def run
11
+ print_welcome_message
16
12
 
17
- def fetch_data(hex1, hex2)
18
- hex1 = convert_3hex_to_6hex(hex1) if hex1.length == 3
19
- hex2 = convert_3hex_to_6hex(hex2) if hex2.length == 3
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
- uri = URI.parse("https://webaim.org/resources/contrastchecker/?fcolor=#{hex1}&bcolor=#{hex2}&api")
22
- response = Net::HTTP.get_response(uri)
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
- JSON.parse(response.body)
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 prompt_input(message)
33
- print message
34
- gets.chomp
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
- def red(string)
38
- "\e[31m#{string}\e[0m"
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
- def green(string)
42
- "\e[32m#{string}\e[0m"
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 colorize_float(float_string)
46
- return red(float_string) if Float(float_string) < 4.5
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
- green(float_string)
67
+ (rgb[:r] * 0.2126 + rgb[:g] * 0.7152 + rgb[:b] * 0.0722).round(4)
49
68
  end
50
69
 
51
- def colorize_status(status_string)
52
- status_string = status_string.upcase
70
+ def pass_or_fail(value, threshold)
71
+ (value >= threshold) ? "PASS" : "FAIL"
72
+ end
53
73
 
54
- return red(status_string) if status_string == "FAIL"
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
- green(status_string)
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
- def run
92
- print_welcome_message
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
- loop do
95
- puts
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
- if !valid_hex?(first) || !valid_hex?(second)
101
- print_error_message
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
- puts
108
- input = prompt_input("\e[33mContinue?\e[0m (\e[32myes\e[0m / \e[31mno\e[0m) ")
109
- break if input.downcase == "no"
110
- end
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.2.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-30 00:00:00.000000000 Z
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: