This repository has been archived by the owner on Jan 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
138 lines (117 loc) · 2.73 KB
/
index.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const Color = require('color')
const { createElement: h } = require('react')
const { renderToStaticMarkup } = require('react-dom/server')
const homepage = 'https://github.com/jxnblk/contrast-swatch'
const parseURL = (req) => {
const { foreground, background, ...query } = req.query
if (!foreground || !background) return null
return {
foreground,
background,
query,
}
}
const HEX = /^[A-Fa-f0-9]{3,6}$/
const getColor = (raw) => {
if (HEX.test(raw)) raw = '#' + raw
try {
return Color(raw)
} catch (e) {
return null
}
}
const getLabel = contrast => {
if (contrast >= 7) return 'AAA'
if (contrast >= 4.5) return 'AA'
if (contrast >= 3) return 'lg'
return 'Fail'
}
const parseColors = (data) => {
const foreground = getColor(data.foreground)
const background = getColor(data.background)
const contrast = foreground.contrast(background)
const label = getLabel(contrast)
return {
foreground,
background,
raw: data,
hex: {
foreground: foreground.hex(),
background: background.hex(),
},
rgb: {
foreground: foreground.rgb().array(),
background: background.rgb().array(),
},
contrast,
label,
}
}
const round = n => Math.floor(100 * n) / 100
const svg = req => {
const data = parseURL(req)
if (!data) return null
const colors = parseColors(data)
const opts = Object.assign({
width: 128,
height: 128,
font: 'system-ui,sans-serif',
fontSize: 1,
fontWeight: 700,
baseline: 0,
label: false,
radius: 0,
}, data.query)
const width = Number(opts.size || opts.width)
const height = Number(opts.size || opts.height)
const xwidth = 32 * (width / height)
const fontSize = Number(opts.fontSize) * 8
let text = [ round(colors.contrast) ]
if (Boolean(opts.label)) {
text.push(colors.label)
}
if (opts.text) text = [opts.text]
const el = h('svg', {
xmlns: 'http://www.w3.org/2000/svg',
width,
height,
viewBox: `0 0 ${xwidth} 32`,
fill: colors.hex.foreground,
style: {
fontFamily: opts.font,
fontWeight: opts.fontWeight,
fontSize,
},
},
h('rect', {
width: xwidth,
height: 32,
fill: colors.hex.background,
rx: opts.radius,
}),
h('text', {
textAnchor: 'middle',
x: xwidth / 2,
y: 16 + Number(opts.baseline),
dominantBaseline: 'middle',
},
text.join(' ')
)
)
const svg = renderToStaticMarkup(el)
return {
...data,
colors,
svg,
}
}
module.exports = async (req, res) => {
const data = svg(req)
if (!data) return
switch (data.query.type) {
case 'json':
return res.send(data)
}
res.setHeader('Content-Type', 'image/svg+xml;charset=utf-8')
res.send(data.svg)
}