mirror of
https://github.com/notion-enhancer/notion-enhancer.git
synced 2025-04-07 05:59:02 +00:00
shade/contrast rgb helpers
This commit is contained in:
parent
9b4b21c297
commit
824da94ffb
41
api/fmt.mjs
41
api/fmt.mjs
@ -93,6 +93,47 @@ export const uuidv4 = () => {
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* log-based shading of an rgb color, from
|
||||
* https://stackoverflow.com/questions/5560248/programmatically-lighten-or-darken-a-hex-color-or-rgb-and-blend-colors
|
||||
* @param {number} p - a decimal amount to shade the color.
|
||||
* 1 = white, 0 = the original color, -1 = black
|
||||
* @param {string} c - the rgb color
|
||||
* @returns {string} the shaded color
|
||||
*/
|
||||
export const rgbLogShade = (p, c) => {
|
||||
var i = parseInt,
|
||||
r = Math.round,
|
||||
[a, b, c, d] = c.split(','),
|
||||
P = p < 0,
|
||||
t = P ? 0 : p * 255 ** 2,
|
||||
P = P ? 1 + p : 1 - p;
|
||||
return (
|
||||
'rgb' +
|
||||
(d ? 'a(' : '(') +
|
||||
r((P * i(a[3] == 'a' ? a.slice(5) : a.slice(4)) ** 2 + t) ** 0.5) +
|
||||
',' +
|
||||
r((P * i(b) ** 2 + t) ** 0.5) +
|
||||
',' +
|
||||
r((P * i(c) ** 2 + t) ** 0.5) +
|
||||
(d ? ',' + d : ')')
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* pick a contrasting color e.g. for text on a variable color background
|
||||
* using the hsp (perceived brightness) constants from http://alienryderflex.com/hsp.html
|
||||
* @param {number} r - red (0-255)
|
||||
* @param {number} g - green (0-255)
|
||||
* @param {number} b - blue (0-255)
|
||||
* @returns {string} the contrasting rgb color, white or black
|
||||
*/
|
||||
export const rgbContrast = (r, g, b) => {
|
||||
return Math.sqrt(0.299 * (r * r) + 0.587 * (g * g) + 0.114 * (b * b)) > 127.5
|
||||
? 'rgb(0,0,0)'
|
||||
: 'rgb(255,255,255)';
|
||||
};
|
||||
|
||||
const patterns = {
|
||||
alphanumeric: /^[\w\.-]+$/,
|
||||
uuid: /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i,
|
||||
|
@ -61,6 +61,9 @@ export const welcomeNotification = {
|
||||
version: env.version,
|
||||
};
|
||||
|
||||
/** the url of a json file online containing notifications pushed out to users */
|
||||
export const notificationsURL = 'https://notion-enhancer.github.io/notifications.json';
|
||||
|
||||
let _list,
|
||||
_errors = [];
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user