-
Notifications
You must be signed in to change notification settings - Fork 2
/
contrastRatio.js
91 lines (71 loc) · 2.86 KB
/
contrastRatio.js
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
79
80
81
82
83
84
85
86
87
88
89
90
/*
This file is part of 'contrast-ratio-extension'.
'contrast-ratio-extension' is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License (version 3) as
published by the Free Software Foundation
'contrast-ratio-extension' is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
'contrast-ratio-extension'. If not, see <http://www.gnu.org/licenses/>.
Copyright (c) 2018 Larissa Reis <[email protected]>
*/
const Lang = imports.lang;
const St = imports.gi.St;
const PopupMenu = imports.ui.popupMenu;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Color = Me.imports.color.Color;
const ContrastRatio = new Lang.Class({
Name: 'ContrastRatio',
Extends: PopupMenu.PopupBaseMenuItem,
_ratioLabel: null,
_scoreLabel: null,
_init: function(backgroundColor, textColor) {
this.parent({ reactive: false, style_class: 'contrast-ratio' });
this._ratioLabel = new St.Label({ reactive: false, style_class: 'ratio-label' })
this._scoreLabel = new St.Label({ reactive: false, style_class: 'score-label' })
this.actor.add(this._scoreLabel, { expand: true, x_align: St.Align.START });
this.actor.add(this._ratioLabel, { expand: true, x_align: St.Align.END });
this.update(backgroundColor, textColor);
},
_calculateContrastRatio: function(backgroundColor, textColor) {
let ratio = (Color.luminance(backgroundColor) + 0.05) / (Color.luminance(textColor) + 0.05);
if (ratio < 1) {
ratio = 1/ratio;
}
// floor with 2 decimal places
return Math.floor(ratio * 100)/100;
},
_scoreFromRatio: function(ratio) {
let levels = {
"Fail": {
lower: 0,
upper: 3
},
"AA-Large": {
lower: 3,
upper: 4.5
},
"AA": {
lower: 4.5,
upper: 7
},
"AAA": {
lower: 7,
upper: 22
}
};
return Object.keys(levels).find(function(key) {
return ratio >= levels[key].lower && ratio < levels[key].upper;
});
},
update: function(backgroundColor, textColor) {
let ratio = this._calculateContrastRatio(backgroundColor, textColor);
let score = this._scoreFromRatio(ratio);
this._ratioLabel.set_text(ratio.toString());
this._scoreLabel.set_text(score);
this.actor.set_style('background-color: ' + backgroundColor + '; color: ' + textColor + ';');
},
});