2023-03-16 11:11:18 +00:00
|
|
|
/**
|
|
|
|
* It returns true if the value is a number, not NaN, and between 0 and 255.
|
|
|
|
* @param value - The value to check.
|
|
|
|
* @returns A function that takes a value and returns a boolean.
|
|
|
|
*/
|
2024-02-19 09:42:59 +00:00
|
|
|
export function isValidRGBValue(value) {
|
2022-04-11 21:57:07 +00:00
|
|
|
return typeof value === 'number' && Number.isNaN(value) === false && value >= 0 && value <= 255;
|
2024-03-19 21:45:17 +00:00
|
|
|
}
|
2020-10-22 00:45:27 +00:00
|
|
|
|
2023-03-16 11:11:18 +00:00
|
|
|
/**
|
|
|
|
* "If the red, green, and blue values are valid, return an object with the red, green, and blue
|
|
|
|
* values, and if the alpha value is valid, add it to the object."
|
|
|
|
*
|
|
|
|
* The function is a bit more complicated than that, but that's the gist of it
|
|
|
|
* @param red - The red value of the color.
|
|
|
|
* @param green - 0-255
|
|
|
|
* @param blue - The blue value of the color.
|
|
|
|
* @param alpha - The alpha value of the color.
|
|
|
|
* @returns An object with the properties red, green, blue, and alpha.
|
|
|
|
*/
|
2024-02-19 09:42:59 +00:00
|
|
|
export function setRGBA(red, green, blue, alpha) {
|
2021-03-17 13:01:53 +00:00
|
|
|
if (isValidRGBValue(red) && isValidRGBValue(green) && isValidRGBValue(blue)) {
|
|
|
|
const color = {
|
|
|
|
red: red | 0,
|
|
|
|
green: green | 0,
|
|
|
|
blue: blue | 0,
|
|
|
|
};
|
2020-10-22 00:45:27 +00:00
|
|
|
|
2021-03-17 13:01:53 +00:00
|
|
|
if (isValidRGBValue(alpha) === true) {
|
|
|
|
color.alpha = alpha | 0;
|
2020-10-22 00:45:27 +00:00
|
|
|
}
|
2021-03-17 13:01:53 +00:00
|
|
|
|
|
|
|
return color;
|
|
|
|
}
|
2020-10-22 00:45:27 +00:00
|
|
|
}
|