forked from geops/gtfsparser
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcolor_utils.go
More file actions
75 lines (66 loc) · 1.69 KB
/
Copy pathcolor_utils.go
File metadata and controls
75 lines (66 loc) · 1.69 KB
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
package gtfsparser
import (
"fmt"
"log"
"math"
"strconv"
)
// Converts a hex color (e.g., "AABBCC") to normalized RGB (0..1)
func hexToRGB(hex string) (r, g, b float64, err error) {
if len(hex) != 6 {
return 0, 0, 0, fmt.Errorf("invalid hex color: %s", hex)
}
parse := func(s string) (float64, error) {
val, err := strconv.ParseInt(s, 16, 64)
if err != nil {
return 0, err
}
return float64(val) / 255.0, nil
}
r, err = parse(hex[0:2])
if err != nil {
return
}
g, err = parse(hex[2:4])
if err != nil {
return
}
b, err = parse(hex[4:6])
if err != nil {
return
}
return
}
// Computes relative luminance using WCAG formula
func relativeLuminance(r, g, b float64) float64 {
convert := func(c float64) float64 {
if c <= 0.03928 {
return c / 12.92
}
return math.Pow((c+0.055)/1.055, 2.4)
}
return 0.2126*convert(r) + 0.7152*convert(g) + 0.0722*convert(b)
}
// Computes contrast ratio between two luminances
func contrastRatio(l1, l2 float64) float64 {
light := math.Max(l1, l2)
dark := math.Min(l1, l2)
return (light + 0.05) / (dark + 0.05)
}
// Checks and prints WCAG contrast evaluation
// See https://www.w3.org/WAI/WCAG21/Understanding/contrast-minimum.html
// "The visual presentation of text and images of text has a contrast ratio of at least 4.5:1""
func hasValidContrast(fgHex, bgHex string) bool {
fgR, fgG, fgB, err := hexToRGB(fgHex)
if err != nil {
log.Fatalf("Invalid foreground color: %v", err)
}
bgR, bgG, bgB, err := hexToRGB(bgHex)
if err != nil {
log.Fatalf("Invalid background color: %v", err)
}
fgLum := relativeLuminance(fgR, fgG, fgB)
bgLum := relativeLuminance(bgR, bgG, bgB)
ratio := contrastRatio(fgLum, bgLum)
return ratio >= 4.5
}