chore: init read-star monorepo

This commit is contained in:
2026-01-16 13:06:44 +08:00
commit 1e510efe8e
508 changed files with 44803 additions and 0 deletions
+93
View File
@@ -0,0 +1,93 @@
import type { AnyColor, HslColor, RgbColor } from 'colord'
import { colord, extend } from 'colord'
import labPlugin from 'colord/plugins/lab'
import mixPlugin from 'colord/plugins/mix'
import namesPlugin from 'colord/plugins/names'
extend([namesPlugin, mixPlugin, labPlugin])
export function isValidColor(color: AnyColor) {
return colord(color).isValid()
}
export function getHex(color: AnyColor) {
return colord(color).toHex()
}
export function getRgb(color: AnyColor) {
return colord(color).toRgb()
}
export function getHsl(color: AnyColor) {
return colord(color).toHsl()
}
export function getHsv(color: AnyColor) {
return colord(color).toHsv()
}
export function getDeltaE(color1: AnyColor, color2: AnyColor) {
return colord(color1).delta(color2)
}
export function transformHslToHex(color: HslColor) {
return colord(color).toHex()
}
/**
* Add color alpha
*
* @param color - Color
* @param alpha - Alpha (0 - 1)
*/
export function addColorAlpha(color: AnyColor, 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: AnyColor, secondColor: AnyColor, 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: AnyColor, 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: AnyColor) {
return colord(color).isEqual('#ffffff')
}
export { colord }
+2
View File
@@ -0,0 +1,2 @@
export * from './colord'
export * from './name'
+50
View File
@@ -0,0 +1,50 @@
import { colorNames } from '../constant'
import { getHex, getHsl, getRgb } from './colord'
/**
* Get color name
*
* @param color
*/
export function getColorName(color: string) {
const hex = getHex(color)
const rgb = getRgb(color)
const hsl = getHsl(color)
let ndf = 0
let ndf1 = 0
let ndf2 = 0
let cl = -1
let df = -1
let name = ''
colorNames.some((item, index) => {
const [hexValue, colorName] = item
const match = hex === hexValue
if (match) {
name = colorName
}
else {
const { r, g, b } = getRgb(hexValue)
const { h, s, l } = getHsl(hexValue)
ndf1 = (rgb.r - r) ** 2 + (rgb.g - g) ** 2 + (rgb.b - b) ** 2
ndf2 = (hsl.h - h) ** 2 + (hsl.s - s) ** 2 + (hsl.l - l) ** 2
ndf = ndf1 + ndf2 * 2
if (df < 0 || df > ndf) {
df = ndf
cl = index
}
}
return match
})
name = colorNames[cl][1]
return name
}