UNPKG

@mirawision/colorize

Version:

A comprehensive color manipulation library for TypeScript, providing functionalities for color conversion, validation, gradient generation, and various color adjustments.

37 lines (36 loc) 1.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.adjustHue = void 0; const convert_color_1 = require("./convert-color"); const get_color_format_1 = require("./get-color-format"); const types_1 = require("./types"); /** * Adjusts the hue of a given color by a specified amount. * * @param {string} color - The color to be adjusted, provided in a format recognized by `getColorFormat`. * This could be in formats like HEX, RGB, or named colors, among others. * If an invalid color format is provided, white (#FFFFFF) will be used as a fallback. * @param {number} amount - The amount to adjust the hue by. This value can be positive (to increase hue) * or negative (to decrease hue). The hue value is constrained between 0 and 360 degrees. * * @returns {string} - The color with adjusted hue, in the same format as the input color. * If the input color format was invalid, returns the adjusted white color in the same format. * * Example usage: * adjustHue('#00FF00', 30); // changes the hue of a bright green color. * adjustHue('rgb(255, 0, 0)', -45); // changes the hue of a red color. * adjustHue('invalidColor', 60); // uses white as fallback and adjusts its hue. */ const adjustHue = (color, amount) => { const colorFormat = (0, get_color_format_1.getColorFormat)(color); // Use white as fallback if color format is invalid const validColor = colorFormat ? color : '#FFFFFF'; const targetFormat = colorFormat || types_1.ColorFormat.HEX; const hslColor = (0, convert_color_1.convertColor)(validColor, types_1.ColorFormat.HSL); let [h, s, l] = hslColor.match(/\d+/g).map(Number); h = (h + amount) % 360; if (h < 0) h += 360; return (0, convert_color_1.convertColor)(`hsl(${h}, ${s}%, ${l}%)`, targetFormat); }; exports.adjustHue = adjustHue;