ruby_color_contrast_checker 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7fd5330a1a1884e0a960e1eac58998c665f7b48a31325f365d01adf7547e73c1
4
- data.tar.gz: 9e5b4cac1f28ba6066903ed0d0ff4a3680f6845e9251a90eb95c435b928b35cf
3
+ metadata.gz: 41127405d0f39eaf9fa5f9ac4743232a3644fe4a0ce8cda366762fc74c4c4a28
4
+ data.tar.gz: d48a9034eb3f53f6fbbf8123c777a0ec53a75037fe8fe82942b9dc228d0cfa64
5
5
  SHA512:
6
- metadata.gz: 68c4a10255ec90887ea0d107c70c969dba2b7d2333d563273b0db94da5e52481c9c8a1c4064a76c45c8fda92727965ab94317696f30b4e5efdaca819990f0802
7
- data.tar.gz: 2af2e9180497133de98f1be80d09c9d36088b8636b83045b53d300d7fe6bf4f12640150cbe92ef6dd305a4c28d50d8965547031eb3d8fb3d7ce05ec208e578f1
6
+ metadata.gz: c5c5cf0bc32f2f378a929e298feedcf589efd4bf2d360b84040947093e8543c35dbb721087eb8192c5fd9607330a56988aafcef223e04f6f46019fdc4047a3bf
7
+ data.tar.gz: d75486f0d90127c0031e9fb8f5575b80a22cd242f30a26f6184c63af7e7f3f6ef0b0599d786da506de5edadaf444e45d84056f13977bc41379cf35fa88f80355
data/Gemfile CHANGED
@@ -4,3 +4,9 @@ source "https://rubygems.org"
4
4
 
5
5
  # Specify your gem's dependencies in ruby_color_contrast_checker.gemspec
6
6
  gemspec
7
+
8
+ gem "rake"
9
+ gem "rspec"
10
+ gem "standard"
11
+ gem "simplecov"
12
+ gem "simplecov-cobertura"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby_color_contrast_checker (0.1.0)
4
+ ruby_color_contrast_checker (0.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -52,6 +52,9 @@ GEM
52
52
  docile (~> 1.1)
53
53
  simplecov-html (~> 0.11)
54
54
  simplecov_json_formatter (~> 0.1)
55
+ simplecov-cobertura (2.1.0)
56
+ rexml
57
+ simplecov (~> 0.19)
55
58
  simplecov-html (0.12.3)
56
59
  simplecov_json_formatter (0.1.4)
57
60
  standard (1.28.4)
@@ -71,11 +74,12 @@ PLATFORMS
71
74
  x86_64-linux
72
75
 
73
76
  DEPENDENCIES
74
- rake (~> 13.0)
75
- rspec (~> 3.0)
77
+ rake
78
+ rspec
76
79
  ruby_color_contrast_checker!
77
- simplecov (~> 0.21.2)
78
- standard (~> 1.3)
80
+ simplecov
81
+ simplecov-cobertura
82
+ standard
79
83
 
80
84
  BUNDLED WITH
81
85
  2.4.7
data/README.md CHANGED
@@ -1,39 +1,136 @@
1
- # RubyColorContrastChecker
1
+ <div align="center">
2
+ <h1 align="center">Ruby Color Contrast Checker</h1>
3
+ <p align="center">CLI interface for checking WCAG2.0 Color Contrast Ratio in Ruby.</p>
4
+ </div>
5
+
6
+ <br />
7
+
8
+ <div align="center">
9
+ <a href="https://badge.fury.io/rb/ruby_color_contrast_checker">
10
+ <img alt="Gem Version" src="https://badge.fury.io/rb/ruby_color_contrast_checker.svg" />
11
+ </a>
12
+ &nbsp;
13
+ <a href="https://github.com/cheehwatang/ruby_color_contrast_checker/blob/main/LICENSE.txt">
14
+ <img alt="GitHub license" src="https://img.shields.io/github/license/cheehwatang/ruby_color_contrast_checker" />
15
+ </a>
16
+ &nbsp;
17
+ <a href="#">
18
+ <img alt="Tests Passing" src="https://github.com/cheehwatang/ruby_color_contrast_checker/workflows/Test/badge.svg" />
19
+ </a>
20
+ &nbsp;
21
+ <a href="#">
22
+ <img alt="Lint Passing" src="https://github.com/cheehwatang/ruby_color_contrast_checker/workflows/Lint/badge.svg" />
23
+ </a>
24
+ &nbsp;
25
+ <a href="#">
26
+ <img alt="Code Coverage Percentage" src="https://codecov.io/gh/cheehwatang/ruby_color_contrast_checker/branch/master/graph/badge.svg" />
27
+ </a>
28
+ &nbsp;
29
+ <a href="https://github.com/standardrb/standard">
30
+ <img alt="Ruby Code Style" src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" />
31
+ </a>
32
+ &nbsp;
33
+ <a href="#">
34
+ <img alt="Repository Top Language" src="https://img.shields.io/github/languages/top/cheehwatang/ruby_color_contrast_checker" />
35
+ </a>
36
+ </div>
37
+
38
+ <br />
39
+
40
+ <hr />
41
+
42
+ <br />
43
+
44
+ Check if the 2 colors provided pass the AA and AAA Levels (including for Large Text and Icons), according to [WCAG 2.1 guidelines](https://www.w3.org/WAI/WCAG21/Understanding/contrast-minimum.html).
45
+
46
+ Further information can be found [here](https://webaim.org/articles/contrast/).
47
+
48
+ In summary:
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
54
+
55
+ Note:
56
+
57
+ Large is for regular text with 18pt or larger, or bold text with 14pt or larger.
58
+
59
+ Logo generally fall into the large category.
60
+
61
+ It is recommended to fulfill at least Level AA for your projects.
62
+
63
+ <br />
2
64
 
3
- TODO: Delete this and the text below, and describe your gem
65
+ ## Installation
4
66
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ruby_color_contrast_checker`. To experiment with that code, run `bin/console` for an interactive prompt.
67
+ Install the gem:
6
68
 
7
- ## Installation
69
+ ```console
70
+ $ gem install ruby_color_contrast_checker
71
+ ```
8
72
 
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
73
+ <br />
10
74
 
11
- Install the gem and add to the application's Gemfile by executing:
75
+ ## Usage
12
76
 
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
77
+ Run the `contrast_checker` executable:
14
78
 
15
- If bundler is not being used to manage dependencies, install the gem by executing:
79
+ ```shell
80
+ $ contrast_checker
16
81
 
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
82
+ ------------------------------------
83
+ | Welcome to Color Contrast Checker |
84
+ ------------------------------------
85
+ \ ^__^
86
+ \ (oo)_______
87
+ (__)\ )\/\
88
+ ||----w |
89
+ || ||
18
90
 
19
- ## Usage
91
+ Enter the first hex color string:
92
+ > #000000
93
+ Enter the second hex color string:
94
+ > #FFFFFF
95
+
96
+ Contrast Ratio : 21
97
+
98
+ Level AA : PASS
99
+ Level AA (Large) : PASS
100
+ Level AAA : PASS
101
+ Level AAA (Large) : PASS
102
+ ```
103
+
104
+ ![Color Contrast Checker CLI](./assets/color_contrast_checker_cli.png)
20
105
 
21
- TODO: Write usage instructions here
106
+ <br />
22
107
 
23
108
  ## Development
24
109
 
25
110
  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.
26
111
 
27
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
112
+ To install this gem onto your local machine, run `bundle exec rake install`.
113
+
114
+ <br />
28
115
 
29
116
  ## Contributing
30
117
 
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ruby_color_contrast_checker. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/ruby_color_contrast_checker/blob/master/CODE_OF_CONDUCT.md).
118
+ Bug reports and pull requests are welcome on GitHub at https://github.com/cheehwatang/ruby_color_contrast_checker. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/cheehwatang/ruby_color_contrast_checker/blob/master/CODE_OF_CONDUCT.md).
119
+
120
+ <br />
32
121
 
33
122
  ## License
34
123
 
35
124
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
36
125
 
126
+ <br />
127
+
37
128
  ## Code of Conduct
38
129
 
39
- Everyone interacting in the RubyColorContrastChecker project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/ruby_color_contrast_checker/blob/master/CODE_OF_CONDUCT.md).
130
+ Everyone interacting in the Ruby Color Contrast Checker project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/cheehwatang/ruby_color_contrast_checker/blob/master/CODE_OF_CONDUCT.md).
131
+
132
+ <br />
133
+
134
+ ## 🌟 Special Thanks
135
+
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.1.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
18
+
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
20
25
 
21
- uri = URI.parse("https://webaim.org/resources/contrastchecker/?fcolor=#{hex1}&bcolor=#{hex2}&api")
22
- response = Net::HTTP.get_response(uri)
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
23
31
 
24
- JSON.parse(response.body)
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,6 +38,54 @@ module RubyColorContrastChecker
29
38
  "#{chars[0]}#{chars[0]}#{chars[1]}#{chars[1]}#{chars[2]}#{chars[2]}"
30
39
  end
31
40
 
41
+ def calculate_contrast_ratio(hex1, hex2)
42
+ rgb1 = convert_6hex_to_rgb_hash(hex1)
43
+ rgb2 = convert_6hex_to_rgb_hash(hex2)
44
+
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)
49
+ end
50
+
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:}
59
+ end
60
+
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
66
+
67
+ (rgb[:r] * 0.2126 + rgb[:g] * 0.7152 + rgb[:b] * 0.0722).round(4)
68
+ end
69
+
70
+ def pass_or_fail(value, threshold)
71
+ (value >= threshold) ? "PASS" : "FAIL"
72
+ end
73
+
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
77
+
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
+
32
89
  def prompt_input(message)
33
90
  print message
34
91
  gets.chomp
@@ -36,34 +93,57 @@ module RubyColorContrastChecker
36
93
 
37
94
  def print_data(data)
38
95
  output = <<~MLS
39
- Contrast Ratio : #{data["ratio"]}
96
+ \e[36mContrast Ratio\e[0m : #{colorize_float(data["ratio"])}
40
97
 
41
- Level AA : #{data["AA"].upcase}
42
- Level AA (Large) : #{data["AALarge"].upcase}
43
- Level AAA : #{data["AAA"].upcase}
44
- Level AAA (Large) : #{data["AAALarge"].upcase}
98
+ \e[36mLevel AA\e[0m : #{colorize_status(data["AA"])}
99
+ \e[36mLevel AA (Large)\e[0m : #{colorize_status(data["AALarge"])}
100
+ \e[36mLevel AAA\e[0m : #{colorize_status(data["AAA"])}
101
+ \e[36mLevel AAA (Large)\e[0m : #{colorize_status(data["AAALarge"])}
45
102
  MLS
46
103
 
47
104
  puts output
48
105
  end
49
106
 
50
- def run
51
- loop do
52
- puts
53
- first = prompt_input("Enter the first hex color string: \n> #")
54
- second = prompt_input("Enter the second hex color string: \n> #")
55
- puts
107
+ def print_welcome_message
108
+ welcome_message = <<~MLS
109
+ \e[36m ------------------------------------ \e[0m
110
+ \e[36m | Welcome to Color Contrast Checker | \e[0m
111
+ \e[36m ------------------------------------ \e[0m
112
+ \\ ^__^
113
+ \\ (oo)_______
114
+ (__)\\ )\\/\\
115
+ ||----w |
116
+ || ||
117
+ MLS
56
118
 
57
- if !valid_hex?(first) || !valid_hex?(second)
58
- puts "The Hex color code is invalid."
59
- else
60
- data = fetch_data(first, second)
61
- print_data(data)
62
- end
119
+ puts welcome_message
120
+ end
63
121
 
64
- puts
65
- input = prompt_input("Continue? (yes / no) ")
66
- break if input.downcase == "no"
67
- end
122
+ def print_error_message
123
+ puts "\e[31mThe Hex color code is invalid.\e[0m"
124
+ end
125
+
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
129
+
130
+ green(float_string)
131
+ end
132
+
133
+ def colorize_status(status_string)
134
+ status_string = status_string.upcase
135
+
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"
68
148
  end
69
149
  end
@@ -29,9 +29,4 @@ Gem::Specification.new do |spec|
29
29
  end
30
30
  spec.executables = ["contrast_checker"]
31
31
  spec.require_paths = ["lib"]
32
-
33
- spec.add_development_dependency "rake", "~> 13.0"
34
- spec.add_development_dependency "rspec", "~> 3.0"
35
- spec.add_development_dependency "standard", "~> 1.3"
36
- spec.add_development_dependency "simplecov", "~> 0.21.2"
37
32
  end
metadata CHANGED
@@ -1,71 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_color_contrast_checker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.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
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: rake
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '13.0'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '13.0'
27
- - !ruby/object:Gem::Dependency
28
- name: rspec
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '3.0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '3.0'
41
- - !ruby/object:Gem::Dependency
42
- name: standard
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '1.3'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '1.3'
55
- - !ruby/object:Gem::Dependency
56
- name: simplecov
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: 0.21.2
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: 0.21.2
11
+ date: 2023-05-31 00:00:00.000000000 Z
12
+ dependencies: []
69
13
  description: 'CLI interface for WCAG Color Contrast Checking in Ruby '
70
14
  email:
71
15
  - cheehwa.tang@gmail.com
@@ -83,6 +27,7 @@ files:
83
27
  - LICENSE.txt
84
28
  - README.md
85
29
  - Rakefile
30
+ - assets/color_contrast_checker_cli.png
86
31
  - bin/contrast_checker
87
32
  - lib/ruby_color_contrast_checker.rb
88
33
  - lib/ruby_color_contrast_checker/version.rb