Files
shields/badge-maker/lib/color.js
dependabot-preview[bot] 501c53a0b4 Build(deps): bump css-color-converter from 1.1.1 to 2.0.0 in /badge-maker (#5615)
* Build(deps): bump css-color-converter in /badge-maker

Bumps [css-color-converter](https://github.com/andyjansson/css-color-converter) from 1.1.1 to 2.0.0.
- [Release notes](https://github.com/andyjansson/css-color-converter/releases)
- [Changelog](https://github.com/andyjansson/css-color-converter/blob/master/CHANGELOG.md)
- [Commits](https://github.com/andyjansson/css-color-converter/commits)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

* Update implementation and package-lock.json

* Fix some URLs which switched to HTTP

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
Co-authored-by: PyvesB <PyvesDev@gmail.com>
2020-09-28 09:16:57 +01:00

89 lines
1.9 KiB
JavaScript

'use strict'
const { fromString } = require('css-color-converter')
// When updating these, be sure also to update the list in `badge-maker/README.md`.
const namedColors = {
brightgreen: '#4c1',
green: '#97ca00',
yellow: '#dfb317',
yellowgreen: '#a4a61d',
orange: '#fe7d37',
red: '#e05d44',
blue: '#007ec6',
grey: '#555',
lightgrey: '#9f9f9f',
}
const aliases = {
gray: 'grey',
lightgray: 'lightgrey',
critical: 'red',
important: 'orange',
success: 'brightgreen',
informational: 'blue',
inactive: 'lightgrey',
}
const resolvedAliases = {}
Object.entries(aliases).forEach(([alias, original]) => {
resolvedAliases[alias] = namedColors[original]
})
// This function returns false for `#ccc`. However `isCSSColor('#ccc')` is
// true.
const hexColorRegex = /^([\da-f]{3}){1,2}$/i
function isHexColor(s = '') {
return hexColorRegex.test(s)
}
function isCSSColor(color) {
return typeof color === 'string' && fromString(color.trim())
}
function normalizeColor(color) {
if (color === undefined) {
return undefined
} else if (color in namedColors) {
return color
} else if (color in aliases) {
return aliases[color]
} else if (isHexColor(color)) {
return `#${color.toLowerCase()}`
} else if (isCSSColor(color)) {
return color.toLowerCase()
} else {
return undefined
}
}
function toSvgColor(color) {
const normalized = normalizeColor(color)
if (normalized in namedColors) {
return namedColors[normalized]
} else if (normalized in resolvedAliases) {
return resolvedAliases[normalized]
} else {
return normalized
}
}
function brightness(color) {
if (color) {
const cssColor = fromString(color)
if (cssColor) {
const rgb = cssColor.toRgbaArray()
return +((rgb[0] * 299 + rgb[1] * 587 + rgb[2] * 114) / 255000).toFixed(2)
}
}
return 0
}
module.exports = {
namedColors,
isHexColor,
normalizeColor,
toSvgColor,
brightness,
}