diff --git a/src/index.d.ts b/src/index.d.ts deleted file mode 100644 index bffe034..0000000 --- a/src/index.d.ts +++ /dev/null @@ -1,12 +0,0 @@ -/** - * Hexashades - create an array of shades & tints for a given color. - * @link https://github.com/arjunkdot/hexashades/ - * @license MIT - */ -export declare class Colors { - #private; - result: string[]; - limit: number; - constructor(); - createColors(color: string, percentage: number): string[]; -} diff --git a/src/index.ts b/src/index.ts index 9a29658..dc4fcd0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ /** * Hexashades - create an array of shades & tints for a given color. - * @version 1.0.0 + * @version 1.0.1 * @link https://github.com/arjunkdot/hexashades/ * @license MIT */ @@ -70,15 +70,17 @@ export class Colors { const hex = this.#rgb2hex(rgbShade); this.result.push(`${this.prefix ? '#' + hex : hex}`); this.limit += percentage; - if (this.limit >= 100) { return; } + this.#generateShades(rgb, percentage); } #getTint(color: number, percentage: number) { - return Math.round(color + (255 - color) * (percentage / 100)); + const tint = Math.round(color + (255 - color) * (percentage / 100)); + // Return the max value 255 in case the calculation yields a number larger than the actual possible output. + return (tint > 255) ? 255 : tint; } #generateTints(rgb: RgbType, percentage: number) { @@ -98,15 +100,15 @@ export class Colors { createColors(color: string, percentage: number, prefix: boolean = false) { // Check if the input exists - if(!color || !percentage){ + if (!color || !percentage) { throw new Error('No input provided'); } // Check for input types - if(typeof color !== 'string' || typeof percentage !== 'number' || typeof prefix !== 'boolean'){ + if (typeof color !== 'string' || typeof percentage !== 'number' || typeof prefix !== 'boolean') { throw new Error('Invalid input. Wrong input types are given.'); } // Check if the percentage is valid - if(percentage < 0 || percentage > 100){ + if (percentage < 0 || percentage > 100) { throw new Error('Invalid input. Invalid percentage value is given.') } // Check if HEX is valid @@ -132,7 +134,7 @@ export class Colors { // Arrange colors in ascending order const tints = this.result.slice(0, this.result.length / 2).reverse(); const shades = this.result.slice(this.result.length / 2, this.result.length).reverse(); - shades.unshift(`${this.prefix ? '#' + color : color}`); + shades.unshift(`${this.prefix ? '#' + color : color}`); // REMOVE THIS COMMENT: Could possibley add the item to the end of the array and then reverse it to make it more fast. // Reset global variables this.limit = 0;