-
Notifications
You must be signed in to change notification settings - Fork 4
/
make-stylesheet
executable file
·78 lines (63 loc) · 2.15 KB
/
make-stylesheet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env ruby
# Usage:
#
# ./make-stylesheet ../original-stylesheets/borland.css ../borland.css
#
# Run from within the tools/ directory
require 'css_parser'
require 'wcag_color_contrast'
require 'chroma'
include CssParser
parser = CssParser::Parser.new
parser.load_file!(ARGV[0]);
converted = File.new("#{ARGV[1]}", "w")
bg_selector = '.highlight'
bg_rules = RuleSet.new(nil, parser.find_by_selector(bg_selector)[0])
calculator = WCAGColorContrast::Ratio.new
bg_color = bg_rules.get_value('background').chomp(';')[1..-1]
if !bg_color
bg_color = bg_rules.get_value('background-color').chomp(';')[1..-1]
end
bg_color_relative_luminance = calculator.relative_luminance(bg_color)
parser.each_selector do |selector, declarations, specificity|
element_bg_color = bg_color
element_bg_color_relative_luminance = bg_color_relative_luminance
rule_set = RuleSet.new(selector, declarations)
if selector == bg_selector
converted.puts rule_set.to_s
next
end
color = rule_set.get_value('color').chomp(';')[1..-1]
if !color
converted.puts rule_set.to_s
next
end
color_relative_luminance = calculator.relative_luminance(color)
selector_bg_color = rule_set.get_value('background-color').chomp(';')[1..-1]
if selector_bg_color
element_bg_color = selector_bg_color
element_bg_color_relative_luminance = calculator.relative_luminance(element_bg_color)
end
ratio = calculator.ratio(color, element_bg_color)
if ratio < 4.5
original = ratio
recommendation = color
while ratio < 4.5
if element_bg_color_relative_luminance < color_relative_luminance
recommendation = recommendation.paint.lighten(1).to_s[1..-1]
else
recommendation = recommendation.paint.darken(1).to_s[1..-1]
end
ratio = calculator.ratio(recommendation, element_bg_color)
end
rule_set.remove_declaration!('color')
rule_set.add_declaration!('color', '#' + recommendation)
puts 'Fixed: ' + selector +
' | Original Ratio: ' + original.to_s +
' | Original Color: ' + color +
' | Background: ' + bg_color +
' | New Color: ' + recommendation
end
converted.puts rule_set.to_s
end
converted.close