Skip to content

Commit

Permalink
refactor(projects): refactor @sa/color-palette => @sa/color & perf @s…
Browse files Browse the repository at this point in the history
…a/utils
  • Loading branch information
honghuangdc committed Apr 25, 2024
1 parent 1cb3816 commit 3499997
Show file tree
Hide file tree
Showing 31 changed files with 269 additions and 171 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

###    💅 Refactors

- **hooks**: refactor @sa/color-palette &nbsp;-&nbsp; by @honghuangdc [<samp>(93191)</samp>](https://github.com/soybeanjs/soybean-admin/commit/9319173)
- **hooks**: refactor @sa/color &nbsp;-&nbsp; by @honghuangdc [<samp>(93191)</samp>](https://github.com/soybeanjs/soybean-admin/commit/9319173)

### &nbsp;&nbsp;&nbsp;📖 Documentation

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"@better-scroll/core": "2.5.1",
"@iconify/vue": "4.1.2",
"@sa/axios": "workspace:*",
"@sa/color-palette": "workspace:*",
"@sa/color": "workspace:*",
"@sa/hooks": "workspace:*",
"@sa/materials": "workspace:*",
"@sa/utils": "workspace:*",
Expand Down
6 changes: 0 additions & 6 deletions packages/color-palette/src/index.ts

This file was deleted.

29 changes: 0 additions & 29 deletions packages/color-palette/src/shared/colord.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@sa/color-palette",
"name": "@sa/color",
"version": "1.0.5",
"exports": {
".": "./src/index.ts"
Expand All @@ -10,6 +10,7 @@
}
},
"dependencies": {
"@sa/utils": "workspace:*",
"colord": "2.9.3"
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions packages/color/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { colorPalettes } from './constant';

export * from './palette';
export * from './shared';
export { colorPalettes };

export * from './types';
110 changes: 17 additions & 93 deletions packages/utils/src/color.ts → packages/color/src/palette/antd.ts
Original file line number Diff line number Diff line change
@@ -1,74 +1,6 @@
import { colord, extend } from 'colord';
import namesPlugin from 'colord/plugins/names';
import mixPlugin from 'colord/plugins/mix';
import type { AnyColor, HsvColor, RgbColor } from 'colord';

extend([namesPlugin, mixPlugin]);

/**
* Add color alpha
*
* @param color - Color
* @param alpha - Alpha (0 - 1)
*/
export function addColorAlpha(color: string, alpha: number) {
return colord(color).alpha(alpha).toHex();
}

/**
* Mix color
*
* @param firstColor - First color
* @param secondColor - Second color
* @param ratio - The ratio of the second color (0 - 1)
*/
export function mixColor(firstColor: string, secondColor: string, ratio: number) {
return colord(firstColor).mix(secondColor, ratio).toHex();
}

/**
* Transform color with opacity to similar color without opacity
*
* @param color - Color
* @param alpha - Alpha (0 - 1)
* @param bgColor Background color (usually white or black)
*/
export function transformColorWithOpacity(color: string, alpha: number, bgColor = '#ffffff') {
const originColor = addColorAlpha(color, alpha);
const { r: oR, g: oG, b: oB } = colord(originColor).toRgb();

const { r: bgR, g: bgG, b: bgB } = colord(bgColor).toRgb();

function calRgb(or: number, bg: number, al: number) {
return bg + (or - bg) * al;
}

const resultRgb: RgbColor = {
r: calRgb(oR, bgR, alpha),
g: calRgb(oG, bgG, alpha),
b: calRgb(oB, bgB, alpha)
};

return colord(resultRgb).toHex();
}

/**
* Is white color
*
* @param color - Color
*/
export function isWhiteColor(color: string) {
return colord(color).isEqual('#ffffff');
}

/**
* Get rgb of color
*
* @param color Color
*/
export function getRgbOfColor(color: string) {
return colord(color).toRgb();
}
import type { AnyColor, HsvColor } from 'colord';
import { getHex, getHsv, isValidColor, mixColor } from '../shared';
import type { ColorIndex } from '../types';

/** Hue step */
const hueStep = 2;
Expand All @@ -86,32 +18,23 @@ const lightColorCount = 5;
const darkColorCount = 4;

/**
* The color index of color palette
*
* From left to right, the color is from light to dark, 6 is main color
*/
type ColorIndex = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;

/**
* Get color palette (from left to right, the color is from light to dark, 6 is main color)
* Get AntD palette color by index
*
* @param color - Color
* @param index - The color index of color palette (the main color index is 6)
* @returns Hex color
*/
export function getColorPalette(color: AnyColor, index: ColorIndex): string {
const transformColor = colord(color);

if (!transformColor.isValid()) {
export function getAntDPaletteColorByIndex(color: AnyColor, index: ColorIndex): string {
if (!isValidColor(color)) {
throw new Error('invalid input color value');
}

if (index === 6) {
return colord(transformColor).toHex();
return getHex(color);
}

const isLight = index < 6;
const hsv = transformColor.toHsv();
const hsv = getHsv(color);
const i = isLight ? lightColorCount + 1 - index : index - lightColorCount - 1;

const newHsv: HsvColor = {
Expand All @@ -120,7 +43,7 @@ export function getColorPalette(color: AnyColor, index: ColorIndex): string {
v: getValue(hsv, i, isLight)
};

return colord(newHsv).toHex();
return getHex(newHsv);
}

/** Map of dark color index and opacity */
Expand All @@ -131,32 +54,33 @@ const darkColorMap = [
{ index: 5, opacity: 0.45 },
{ index: 5, opacity: 0.65 },
{ index: 5, opacity: 0.85 },
{ index: 4, opacity: 0.9 },
{ index: 5, opacity: 0.9 },
{ index: 4, opacity: 0.93 },
{ index: 3, opacity: 0.95 },
{ index: 2, opacity: 0.97 },
{ index: 1, opacity: 0.98 }
];

/**
* Get color palettes
* Get AntD color palette
*
* @param color - Color
* @param darkTheme - Dark theme
* @param darkThemeMixColor - Dark theme mix color (default: #141414)
*/
export function getColorPalettes(color: AnyColor, darkTheme = false, darkThemeMixColor = '#141414'): string[] {
const indexes: ColorIndex[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
export function getAntDColorPalette(color: AnyColor, darkTheme = false, darkThemeMixColor = '#141414'): string[] {
const indexes: ColorIndex[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];

const patterns = indexes.map(index => getColorPalette(color, index));
const patterns = indexes.map(index => getAntDPaletteColorByIndex(color, index));

if (darkTheme) {
const darkPatterns = darkColorMap.map(({ index, opacity }) => {
const darkColor = colord(darkThemeMixColor).mix(patterns[index], opacity);
const darkColor = mixColor(darkThemeMixColor, patterns[index], opacity);

return darkColor;
});

return darkPatterns.map(item => colord(item).toHex());
return darkPatterns.map(item => getHex(item));
}

return patterns;
Expand Down
45 changes: 45 additions & 0 deletions packages/color/src/palette/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { AnyColor } from 'colord';
import { getHex } from '../shared';
import type { ColorPaletteNumber } from '../types';
import { getRecommendedColorPalette } from './recommend';
import { getAntDColorPalette } from './antd';

/**
* get color palette by provided color
*
* @param color
* @param recommended whether to get recommended color palette (the provided color may not be the main color)
*/
export function getColorPalette(color: AnyColor, recommended = false) {
const colorMap = new Map<ColorPaletteNumber, string>();

if (recommended) {
const colorPalette = getRecommendedColorPalette(getHex(color));
colorPalette.palettes.forEach(palette => {
colorMap.set(palette.number, palette.hex);
});
} else {
const colors = getAntDColorPalette(color);

const colorNumbers: ColorPaletteNumber[] = [50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950];

colorNumbers.forEach((number, index) => {
colorMap.set(number, colors[index]);
});
}

return colorMap;
}

/**
* get color palette color by number
*
* @param color the provided color
* @param number the color palette number
* @param recommended whether to get recommended color palette (the provided color may not be the main color)
*/
export function getPaletteColorByNumber(color: AnyColor, number: ColorPaletteNumber, recommended = false) {
const colorMap = getColorPalette(color, recommended);

return colorMap.get(number as ColorPaletteNumber)!;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import type {
} from '../types';

/**
* get color palette by provided color and color name
* get recommended color palette by provided color
*
* @param color the provided color
*/
export function getColorPalette(color: string) {
const colorPaletteFamily = getColorPaletteFamily(color);
export function getRecommendedColorPalette(color: string) {
const colorPaletteFamily = getRecommendedColorPaletteFamily(color);

const colorMap = new Map<ColorPaletteNumber, ColorPalette>();

Expand All @@ -36,13 +36,13 @@ export function getColorPalette(color: string) {
}

/**
* get color by number
* get recommended palette color by provided color
*
* @param color the provided color
* @param number the color palette number
*/
export function getColorByPaletteNumber(color: string, number: ColorPaletteNumber) {
const colorPalette = getColorPalette(color);
export function getRecommendedPaletteColorByNumber(color: string, number: ColorPaletteNumber) {
const colorPalette = getRecommendedColorPalette(color);

const { hex } = colorPalette.colorMap.get(number)!;

Expand All @@ -54,7 +54,7 @@ export function getColorByPaletteNumber(color: string, number: ColorPaletteNumbe
*
* @param color the provided color
*/
export function getColorPaletteFamily(color: string) {
export function getRecommendedColorPaletteFamily(color: string) {
if (!isValidColor(color)) {
throw new Error('Invalid color, please check color value!');
}
Expand Down

0 comments on commit 3499997

Please sign in to comment.