Skip to content

Commit

Permalink
feat: update file system and fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ibodev1 committed May 15, 2023
1 parent 4bcba8d commit 0e08e9b
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 239 deletions.
27 changes: 27 additions & 0 deletions src/colorResult.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import chroma from "chroma-js";
import type { IColorResultOptions } from "./types";

const darkenValue = (shade: number, mainShade: number) => {
return (shade - mainShade) / 100 / 2;
}
const shadeColor = (primaryColor: string, mainShade: number, shade: number) => {
return chroma(primaryColor)
.darken(darkenValue(shade, mainShade))
.hex();
}
const shadeColorResult = (fn: any, options: IColorResultOptions) => {
return options.shades.reduce((acc: any, shade: number) => {
acc[shade] = fn(options.primaryColor, options.mainShade, shade);
return acc;
}, {});
}
const colorResult = (options: IColorResultOptions) => {
const palette = shadeColorResult(shadeColor, options);
const colorPalette = {
...palette,
DEFAULT: options.primaryColor
};
return Object.freeze(colorPalette) ?? {};
}

export default colorResult;
66 changes: 66 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import type { IColorResultOptions, Palette } from "./types";

export const initalOptions: IColorResultOptions = {
mainShade: 500,
primaryColor: "#FFBD00",
shades: [50, 100, 200, 300, 400, 500, 600, 700, 800, 900]
}

export const isColor = (color: string) => {
const reg = /^#([\da-f]{3}){1,2}$|^#([\da-f]{6}){1,2}$|(rgb|hsl)a?\((\s*-?\d+%?\s*,){2}(\s*-?\d+%?\s*,?\s*\)?)(,\s*(0?\.\d+)?|1)?\)/gim;
if (typeof color === "string" && reg.test(color)) {
return true;
} else {
return false;
}
}

export const checkParam = (palette: Palette) => {
if (
palette.color &&
typeof palette.color === "string" &&
palette.name &&
typeof palette.name == "string"
) {
if (!isColor(palette.color)) {
throw new Error(
`'${palette.color}' The value you entered is not a color. e.g #ffbd00 or #ffb or rgba(255, 189, 0, 1) or rgb(255, 189, 0) or hsl(44, 100%, 50%)`
);
} else if (!palette.shade && palette.shades) {
throw new Error(
`If you want to specify the shades, you have to specify the main shade.`
);
} else if (palette.shade && typeof palette.shade !== "number") {
throw new Error(
`'${palette.shade}' - type: ${typeof palette.shade} It must be of type number.`
);
} else if (
palette.shades &&
!Array.isArray(palette.shades)
) {
throw new Error(`Shades are not array.`);
} else if (palette.shades && palette.shades.length <= 2) {
throw new Error(`Shades can consist of at least 3 elements.`);
} else if (
palette.shade &&
palette.shades &&
!palette.shades.includes(palette.shade)
) {
throw new Error(
`'${palette.shade}' mainShade are not included in the your shades.`
);
} else if (
!palette.shades &&
palette.shade &&
!initalOptions.shades.includes(palette.shade)
) {
throw new Error(
`'${palette.shade}' mainShade can only be 50, 100, 200, 300, 400, 500, 600, 700, 800 or 900.`
);
} else {
return true;
}
} else {
throw new Error("Make sure the required data is included.");
}
}
160 changes: 26 additions & 134 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,149 +1,41 @@
import chroma from "chroma-js";
import type { IColorResultOptions, Palette } from "./types";
import colorResult from "./colorResult";
import { checkParam, initalOptions } from "./helpers";

export interface Palette {
name: string;
color: string;
shade?: number;
shades?: number[];
}

const main = {
state: {
initialParams: {
color: "#FFBD00",
name: "primary",
shade: 500,
shades: [50, 100, 200, 300, 400, 500, 600, 700, 800, 900]
},
primaryColor: "#FFBD00",
shades: [50, 100, 200, 300, 400, 500, 600, 700, 800, 900],
mainShade: 500
},
getters: {
darkenValue: (shade: number) => {
return (shade - main.state.mainShade) / 100 / 2;
},
shadeColor: (shade: number) => {
return chroma(main.state.primaryColor)
.darken(main.getters.darkenValue(shade))
.hex();
},
shadeColorResult: (fn: any) => {
return main.state.shades.reduce((acc: any, shade: number) => {
acc[shade] = fn(shade);
return acc;
}, {});
},
colorResult: () => {
const palette = main.getters.shadeColorResult(main.getters.shadeColor);
const DEFAULT = main.getters.shadeColor(main.state.mainShade);
const colorPalette = {
...palette,
DEFAULT: DEFAULT
};
return Object.freeze(colorPalette) ?? {};
}
},
checks: {
checkParam: (palette: Palette) => {
if (
palette.color &&
typeof palette.color === "string" &&
palette.name &&
typeof palette.name == "string"
) {
if (!main.checks.checkColor(palette.color)) {
throw new Error(
`'${palette.color}' The value you entered is not a color. e.g #ffbd00 or #ffb or rgba(255, 189, 0, 1) or rgb(255, 189, 0) or hsl(44, 100%, 50%)`
);
} else if (!palette.shade && palette.shades) {
throw new Error(
`If you want to specify the shades, you have to specify the main shade.`
);
} else if (palette.shade && typeof palette.shade !== "number") {
throw new Error(
`'${palette.shade}' - type: ${typeof palette.shade} It must be of type number.`
);
} else if (
palette.shades &&
!Array.isArray(palette.shades)
) {
throw new Error(`Shades are not array.`);
} else if (palette.shades && palette.shades.length <= 2) {
throw new Error(`Shades can consist of at least 3 elements.`);
} else if (
palette.shade &&
palette.shades &&
!palette.shades.includes(palette.shade)
) {
throw new Error(
`'${palette.shade}' mainShade are not included in the your shades.`
);
} else if (
!palette.shades &&
palette.shade &&
!main.state.initialParams.shades.includes(palette.shade)
) {
throw new Error(
`'${palette.shade}' mainShade can only be 50, 100, 200, 300, 400, 500, 600, 700, 800 or 900.`
);
} else {
return true;
}
} else {
throw new Error("Make sure the required data is included.");
}
},
checkColor: (color: string) => {
var reg = /^#([\da-f]{3}){1,2}$|^#([\da-f]{6}){1,2}$|(rgb|hsl)a?\((\s*-?\d+%?\s*,){2}(\s*-?\d+%?\s*,?\s*\)?)(,\s*(0?\.\d+)?|1)?\)/gim;
if (typeof color === "string" && reg.test(color)) {
return true;
} else {
return false;
}
}
}
};

const getPalette = (
params: Palette[] | Palette | string = main.state.initialParams
): any => {
const getPalette = (params: Palette[] | Palette | string): any => {
let palette: any = {};
main.state.primaryColor = "#FFBD00";
main.state.mainShade = 500;
main.state.shades = [50, 100, 200, 300, 400, 500, 600, 700, 800, 900];
if (Array.isArray(params)) {
for (let index = 0; index < params.length; index++) {
const colorPalette = params[index];
if (main.checks.checkParam(colorPalette)) {
main.state.primaryColor = chroma(colorPalette.color).hex();
main.state.mainShade = colorPalette.shade || main.state.mainShade;
main.state.shades = colorPalette.shades || main.state.shades;
let result = main.getters.colorResult();

palette[colorPalette.name] = result;
for (let i = 0; i < params.length; i++) {
const colorPalette = params[i];
if (checkParam(colorPalette)) {
const options: IColorResultOptions = {
mainShade: colorPalette.shade ?? initalOptions.mainShade,
primaryColor: chroma(colorPalette.color).hex() ?? initalOptions.primaryColor,
shades: colorPalette.shades ?? initalOptions.shades
};

palette[colorPalette.name] = colorResult(options);
}
}
return palette;
} else if (typeof params !== "string" && !Array.isArray(params)) {
if (main.checks.checkParam(params)) {
main.state.primaryColor = chroma(params.color).hex();
main.state.mainShade = params.shade || main.state.mainShade;
main.state.shades = params.shades || main.state.shades;
let result = main.getters.colorResult();

palette[params.name] = result;
if (checkParam(params)) {
const options: IColorResultOptions = {
mainShade: params.shade ?? initalOptions.mainShade,
primaryColor: chroma(params.color).hex() ?? initalOptions.primaryColor,
shades: params.shades ?? initalOptions.shades
};

return palette;
palette[params.name] = colorResult(options);
}
} else if (typeof params === "string") {
main.state.primaryColor = chroma(params.toString()).hex();
let result = main.getters.colorResult();

palette[main.state.initialParams.name] = result;
const options: IColorResultOptions = Object.assign(initalOptions, {
primaryColor: chroma(params).hex()
});

return palette;
palette["primary"] = colorResult(options);
}
return palette;
};

export default getPalette;
12 changes: 12 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export interface IColorResultOptions {
primaryColor: string;
mainShade: number;
shades: number[]
}

export interface Palette {
name: string;
color: string;
shade?: number;
shades?: number[];
}
Loading

0 comments on commit 0e08e9b

Please sign in to comment.