Repository navigation
Expand file tree
/
Copy pathlogos.js
More file actions
131 lines (110 loc) · 2.99 KB
/
Copy pathlogos.js
File metadata and controls
131 lines (110 loc) · 2.99 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
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
'use strict'
const { toSvgColor } = require('../gh-badges/lib/color')
const coalesce = require('../core/base-service/coalesce')
const { svg2base64 } = require('./svg-helpers')
const logos = require('./load-logos')()
const simpleIcons = require('./load-simple-icons')()
function prependPrefix(s, prefix) {
if (s === undefined) {
return undefined
}
s = `${s}`
if (s.startsWith(prefix)) {
return s
} else {
return prefix + s
}
}
function isDataUrl(s) {
return s !== undefined && /^(data:)([^;]+);([^,]+),(.+)$/.test(s)
}
// +'s are replaced with spaces when used in query params, this returns them
// to +'s, then removes remaining whitespace.
// https://github.com/badges/shields/pull/1546
function decodeDataUrlFromQueryParam(value) {
if (typeof value !== 'string') {
return undefined
}
const maybeDataUrl = prependPrefix(value, 'data:')
.replace(/ /g, '+')
.replace(/\s/g, '')
return isDataUrl(maybeDataUrl) ? maybeDataUrl : undefined
}
function getShieldsIcon({ name, color }) {
const key = name.toLowerCase()
if (!(key in logos)) {
return undefined
}
const { svg, base64 } = logos[key]
const svgColor = toSvgColor(color)
if (svgColor) {
return svg2base64(svg.replace(/fill="(.+?)"/g, `fill="${svgColor}"`))
} else {
return base64
}
}
function brightness({ r, g, b }) {
return +((r * 299 + g * 587 + b * 114) / 255000).toFixed(2)
}
const hexColorRegex = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i
function hexToRgb(hex) {
const result = hexColorRegex.exec(hex)
return {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16),
}
}
function getSimpleIconStyle({ icon, style }) {
const { hex } = icon
if (style !== 'social' && brightness(hexToRgb(hex)) <= 0.4) {
return 'light'
}
if (style === 'social' && brightness(hexToRgb(hex)) >= 0.6) {
return 'dark'
}
return 'default'
}
function getSimpleIcon({ name, color, style }) {
const key = name.toLowerCase().replace(/ /g, '-')
if (!(key in simpleIcons)) {
return undefined
}
const svgColor = toSvgColor(color)
if (svgColor) {
return svg2base64(
simpleIcons[key].svg.replace('<svg', `<svg fill="${svgColor}"`)
)
} else {
const iconStyle = getSimpleIconStyle({ icon: simpleIcons[key], style })
return simpleIcons[key].base64[iconStyle]
}
}
function prepareNamedLogo({ name, color, style }) {
if (typeof name !== 'string') {
return undefined
}
return (
getShieldsIcon({ name, color }) || getSimpleIcon({ name, color, style })
)
}
function makeLogo(defaultNamedLogo, overrides) {
const maybeDataUrl = decodeDataUrlFromQueryParam(overrides.logo)
if (maybeDataUrl) {
return maybeDataUrl
} else {
return prepareNamedLogo({
name: coalesce(overrides.logo, defaultNamedLogo),
color: overrides.logoColor,
style: overrides.style,
})
}
}
module.exports = {
prependPrefix,
isDataUrl,
decodeDataUrlFromQueryParam,
prepareNamedLogo,
getShieldsIcon,
makeLogo,
}