Skip to content

Commit

Permalink
fix: miscalculation while generating tints.
Browse files Browse the repository at this point in the history
  • Loading branch information
arjunkdot committed Oct 29, 2023
1 parent 93f728a commit 6eeb44f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 19 deletions.
12 changes: 0 additions & 12 deletions src/index.d.ts

This file was deleted.

16 changes: 9 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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;
Expand Down

0 comments on commit 6eeb44f

Please sign in to comment.