Skip to content

Commit

Permalink
refactor: ♻️ Remove ! from algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
dhnchandan committed Mar 23, 2023
1 parent 5be83c0 commit 3f49aef
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 49 deletions.
55 changes: 29 additions & 26 deletions src/algorithoms.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BlendMode, HEX, HSL, RGB } from "./types";
import { modeMapping, safeAlpha, safeHex, safeRgb } from "./utils";
import {modeMapping, safeAlpha, safeHex, safePct, safeRgb} from "./utils";

export const normal = (source: number, ref: number): number => ref + source * 0;
export const multiply = (source: number, ref: number): number => source * ref;
Expand Down Expand Up @@ -108,15 +108,15 @@ export const hslToRgb = (h: number, s: number, l: number, a: number): RGB => {
export const stringToRgb = (c: string, alpha: boolean): RGB => {
const slice: number = alpha ? 5 : 4;
const sep: string = c.indexOf(",") > -1 ? "," : " ";
const rgba: string[] = c.slice(slice).split(")")[0]!.split(sep);
const rgba: string[] = c.slice(slice).split(")")[0].split(sep);

if (rgba.indexOf("/") > -1) rgba.splice(3, 1);

for (let i: number = 0; i < rgba.length; i++) {
const r: string = rgba[i]!;
const r: string = rgba[i];

if (r.indexOf("%") > -1) {
const p: number = Number(r.replace("%", "")) / 100;
const p: number = safePct(Number(r.replace("%", ""))) / 100;
if (i < 3) rgba[i] = String(Math.round(p * 255));
else rgba[i] = String(p);
} else {
Expand All @@ -130,17 +130,17 @@ export const stringToRgb = (c: string, alpha: boolean): RGB => {
export const stringToHsl = (c: string, alpha: boolean): HSL => {
const slice: number = alpha ? 5 : 4;
const sep: string = c.indexOf(",") > -1 ? "," : " ";
const hsla: string[] = c.slice(slice).split(")")[0]!.split(sep);
const hsla: string[] = c.slice(slice).split(")")[0].split(sep);

if (hsla.indexOf("/") > -1) hsla.splice(3, 1);

let h: string = hsla[0]!,
s: string = hsla[1]!.replace("%", ""),
l: string = hsla[2]!.replace("%", ""),
a: string = alpha ? hsla[3]! : "1";
let h: string = hsla[0],
s: string = hsla[1].replace("%", ""),
l: string = hsla[2].replace("%", ""),
a: string = alpha ? hsla[3] : "1";

if (a.indexOf("%") > -1) {
a = String(Number(a.replace("%", "")) / 100);
a = String(safePct(Number(a.replace("%", ""))) / 100);
}

if (h.indexOf("deg") > -1) h = h.replace("deg", "");
Expand All @@ -155,26 +155,26 @@ export const stringToHex = (hex: string): HEX => {
let x: string = "00", y: string = "00", z: string = "00", a: string = "ff";

if (len === 3 || len === 4) {
const result: RegExpExecArray | null = /^#?([a-f\d]{1})([a-f\d]{1})([a-f\d]{1})$/i.exec(hex);
const result: RegExpExecArray | null = /^#?([a-f\d])([a-f\d])([a-f\d])$/i.exec(hex);
if (result) {
x = result[1]! + result[1]!;
y = result[2]! + result[2]!;
z = result[3]! + result[3]!;
x = result[1] + result[1];
y = result[2] + result[2];
z = result[3] + result[3];
}
} else if (len === 6 || len === 7) {
const result: RegExpExecArray | null = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
if (result) {
x = result[1]!;
y = result[2]!;
z = result[3]!;
x = result[1];
y = result[2];
z = result[3];
}
} else if (len === 8 || len === 9) {
const result: RegExpExecArray | null = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
if (result) {
x = result[1]!;
y = result[2]!;
z = result[3]!;
a = result[4]!;
x = result[1];
y = result[2];
z = result[3];
a = result[4];
}
}

Expand All @@ -187,10 +187,13 @@ export const colorChannelMixer = (colorChannelA: number, colorChannelB: number,
return channelA + channelB;
}

export const blenderCb = (source: [number, number, number], ref: [number, number, number], mode: BlendMode): RGB => {
const r: number = separableBlend(mode, source[0], ref[0]);
const g: number = separableBlend(mode, source[1], ref[1]);
const b: number = separableBlend(mode, source[2], ref[0]);
export const blendAlpha = (s: number, r: number): number => Number((s + r - s * r).toFixed(2))

return { r, g, b };
export const blenderCb = (source: RGB, ref: RGB, mode: BlendMode): RGB => {
const r: number = separableBlend(mode, source.r, ref.r);
const g: number = separableBlend(mode, source.g, ref.g);
const b: number = separableBlend(mode, source.b, ref.b);
const a: number = blendAlpha(source.a, ref.a);

return { r, g, b, a };
}
35 changes: 15 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class Color {
const col: string = new Color().base(c).toHex(false);
const arr: [string, string][] = Object.entries(new Color().colorNames);
const index: number = arr.findIndex(c => c[1] === col);
return index > -1 ? arr[index]![0] : col;
return index > -1 ? arr[index][0] : col;
}

public static validate(c: string | NamedColor): ValidationResult {
Expand Down Expand Up @@ -194,14 +194,9 @@ export class Color {
public static blend(source: PossibleColors, ref: PossibleColors, mode: BlendMode = "normal"): Color {
const sourceC: Color = new Color().base(source);
const refC: Color = new Color().base(ref);
const a: number = Number((sourceC.rgba.a! + refC.rgba.a! - sourceC.rgba.a! * refC.rgba.a!).toFixed(2));
const rgb: RGB = blenderCb(
[sourceC.rgba.r, sourceC.rgba.g, sourceC.rgba.b],
[refC.rgba.r, refC.rgba.g, refC.rgba.b],
mode
);
const rgb: RGB = blenderCb(sourceC.toRgbObj(), refC.toRgbObj(), mode);

return new Color().rgb(rgb.r, rgb.g, rgb.b, a);
return new Color().rgb(rgb.r, rgb.g, rgb.b, rgb.a);
}

public getName(): PossibleColorStrings {
Expand Down Expand Up @@ -246,9 +241,9 @@ export class Color {
if (valid_c[1].method === "hex") {
this.hex(c);
} else if (valid_c[1].method === "hsl") {
this.fromHslString(c, valid_c[1].alpha!);
this.fromHslString(c, valid_c[1].alpha);
} else if (valid_c[1].method === "rgb") {
this.fromRgbString(c, valid_c[1].alpha!);
this.fromRgbString(c, valid_c[1].alpha);
} else if (valid_c[1].method === "css_name") {
this.name(c as NamedColor);
}
Expand Down Expand Up @@ -347,14 +342,14 @@ export class Color {
}

public fade(value: number): Color {
const v: number = this.hsla.a! * (1 - (safePct(value) / 100));
const v: number = this.hsla.a * (1 - (safePct(value) / 100));
this.hsl(this.hsla.h, this.hsla.s, this.hsla.l, v);

return this;
}

public brighten(value: number): Color {
const v: number = this.hsla.a! * (1 + (safePct(value) / 100));
const v: number = this.hsla.a * (1 + (safePct(value) / 100));
this.hsl(this.hsla.h, this.hsla.s, this.hsla.l, Number(v.toFixed(2)));

return this;
Expand Down Expand Up @@ -516,13 +511,13 @@ export class Color {

public toRgb(withAlpha: boolean = true, withPct: boolean = false): string {
let r: number = this.rgba.r, g: number = this.rgba.g, b: number = this.rgba.b,
a: number = this.rgba.a as number;
a: number = this.rgba.a;

if (withPct) {
r = Number((r / 255 * 100).toFixed(1));
g = Number((g / 255 * 100).toFixed(1));
b = Number((b / 255 * 100).toFixed(1));
a = Number((a! * 100).toFixed(1));
a = Number((a * 100).toFixed(1));

if (withAlpha) return `rgba(${r}%, ${g}%, ${b}%, ${a}%)`; else return `rgb(${r}%, ${g}%, ${b}%)`;
}
Expand Down Expand Up @@ -623,7 +618,7 @@ export class Color {
r: safeRgb(rgb.r),
g: safeRgb(rgb.g),
b: safeRgb(rgb.b),
a: rgb.a !== undefined ? safeAlpha(rgb.a) : this.rgba.a!,
a: safeAlpha(rgb.a),
};

return this;
Expand All @@ -641,7 +636,7 @@ export class Color {
h: safeHue(hsl.h),
s: safePct(hsl.s),
l: safePct(hsl.l),
a: hsl.a !== undefined ? safeAlpha(hsl.a) : this.hsla.a!,
a: safeAlpha(hsl.a),
};

return this;
Expand All @@ -666,7 +661,7 @@ export class Color {
x: toHexCh(this.rgba.r),
y: toHexCh(this.rgba.g),
z: toHexCh(this.rgba.b),
a: toHexCh(toB255Alpha(this.rgba.a!)),
a: toHexCh(toB255Alpha(this.rgba.a)),
};

return this;
Expand All @@ -677,20 +672,20 @@ export class Color {
r: toB16Ch(this.hexa.x),
g: toB16Ch(this.hexa.y),
b: toB16Ch(this.hexa.z),
a: toB10Alpha(toB16Ch(this.hexa.a!)),
a: toB10Alpha(toB16Ch(this.hexa.a)),
};

return this;
}

private rgbToHsl(): Color {
this.hsla = rgbToHsl(this.rgba.r, this.rgba.g, this.rgba.b, this.rgba.a!);
this.hsla = rgbToHsl(this.rgba.r, this.rgba.g, this.rgba.b, this.rgba.a);

return this;
}

private hslToRgb(): Color {
this.rgba = hslToRgb(this.hsla.h, this.hsla.s, this.hsla.l, this.hsla.a!);
this.rgba = hslToRgb(this.hsla.h, this.hsla.s, this.hsla.l, this.hsla.a);

return this;
}
Expand Down
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ export interface RGB {
r: number;
g: number;
b: number;
a?: number;
a: number;
}

export interface HSL {
h: number;
s: number;
l: number;
a?: number;
a: number;
}

export interface HEX {
x: string;
y: string;
z: string;
a?: string;
a: string;
}

export type NamedColor =
Expand Down

0 comments on commit 3f49aef

Please sign in to comment.