diff --git a/babel.config.js b/babel.config.js deleted file mode 100644 index e6ffbd4..0000000 --- a/babel.config.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = { - presets: [['@babel/preset-env', { targets: { node: 'current' } }], '@babel/preset-typescript'], -}; diff --git a/jest.config.js b/jest.config.js index 8e9bb3a..c68b62b 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,5 +1,4 @@ module.exports = { + preset: 'ts-jest', collectCoverage: true, - // transform @ctrl/tinycolor imports from node_modules - transformIgnorePatterns: [], }; diff --git a/package.json b/package.json index 279eb32..d51f14f 100644 --- a/package.json +++ b/package.json @@ -35,8 +35,6 @@ "@ctrl/tinycolor": "^3.3.1" }, "devDependencies": { - "@babel/preset-env": "^7.12.13", - "@babel/preset-typescript": "^7.12.13", "@types/jest": "^26.0.0", "@typescript-eslint/eslint-plugin": "^4.0.0", "@typescript-eslint/parser": "^4.7.0", @@ -48,6 +46,7 @@ "jest": "^26.0.1", "np": "^7.0.0", "prettier": "^2.0.0", + "ts-jest": "^26.0.0", "typescript": "^4.0.2" }, "homepage": "https://github.com/ant-design/ant-design-colors#readme" diff --git a/src/generate.ts b/src/generate.ts index f8ea116..6ea6086 100644 --- a/src/generate.ts +++ b/src/generate.ts @@ -1,5 +1,4 @@ -import { rgbToHsv, rgbToHex } from '@ctrl/tinycolor/dist/module/conversion'; -import { inputToRGB } from '@ctrl/tinycolor/dist/module/format-input'; +import { TinyColor } from '@ctrl/tinycolor'; const hueStep = 2; // 色相阶梯 const saturationStep = 0.16; // 饱和度阶梯,浅色部分 @@ -28,38 +27,6 @@ interface HsvObject { v: number; } -interface RgbObject { - r: number; - g: number; - b: number; -} - -// Wrapper function ported from TinyColor.prototype.toHsv -// Keep it here because of `hsv.h * 360` -function toHsv({ r, g, b }: RgbObject): HsvObject { - const hsv = rgbToHsv(r, g, b); - return { h: hsv.h * 360, s: hsv.s, v: hsv.v }; -} - -// Wrapper function ported from TinyColor.prototype.toHexString -// Keep it here because of the prefix `#` -function toHex({ r, g, b }: RgbObject): string { - return `#${rgbToHex(r, g, b, false)}`; -} - -// Wrapper function ported from TinyColor.prototype.mix, not treeshakable. -// Amount in range [0, 1] -// Assume color1 & color2 has no alpha, since the following src code did so. -function mix(rgb1: RgbObject, rgb2: RgbObject, amount: number): RgbObject { - const p = amount / 100; - const rgb = { - r: (rgb2.r - rgb1.r) * p + rgb1.r, - g: (rgb2.g - rgb1.g) * p + rgb1.g, - b: (rgb2.b - rgb1.b) * p + rgb1.b, - }; - return rgb; -} - function getHue(hsv: HsvObject, i: number, light?: boolean): number { let hue: number; // 根据色相不同,色相转向不同 @@ -123,37 +90,33 @@ interface Opts { export default function generate(color: string, opts: Opts = {}): string[] { const patterns: Array = []; - const pColor = inputToRGB(color); + const pColor = new TinyColor(color); for (let i = lightColorCount; i > 0; i -= 1) { - const hsv = toHsv(pColor); - const colorString: string = toHex( - inputToRGB({ - h: getHue(hsv, i, true), - s: getSaturation(hsv, i, true), - v: getValue(hsv, i, true), - }), - ); + const hsv = pColor.toHsv(); + const colorString: string = new TinyColor({ + h: getHue(hsv, i, true), + s: getSaturation(hsv, i, true), + v: getValue(hsv, i, true), + }).toHexString(); patterns.push(colorString); } - patterns.push(toHex(pColor)); + patterns.push(pColor.toHexString()); for (let i = 1; i <= darkColorCount; i += 1) { - const hsv = toHsv(pColor); - const colorString: string = toHex( - inputToRGB({ - h: getHue(hsv, i), - s: getSaturation(hsv, i), - v: getValue(hsv, i), - }), - ); + const hsv = pColor.toHsv(); + const colorString: string = new TinyColor({ + h: getHue(hsv, i), + s: getSaturation(hsv, i), + v: getValue(hsv, i), + }).toHexString(); patterns.push(colorString); } // dark theme patterns if (opts.theme === 'dark') { return darkColorMap.map(({ index, opacity }) => { - const darkColorString: string = toHex( - mix(inputToRGB(opts.backgroundColor || '#141414'), inputToRGB(patterns[index]), opacity * 100), - ); + const darkColorString: string = new TinyColor(opts.backgroundColor || '#141414') + .mix(patterns[index], opacity * 100) + .toHexString(); return darkColorString; }); } diff --git a/src/tinycolor.d.ts b/src/tinycolor.d.ts deleted file mode 100644 index 382cdeb..0000000 --- a/src/tinycolor.d.ts +++ /dev/null @@ -1,14 +0,0 @@ -// Declare the modules here since the library only has dts files on cjs files. -// Maybe someone can create a PR later. - -declare module '@ctrl/tinycolor/dist/module/conversion' { - export * from '@ctrl/tinycolor/dist/conversion'; -} - -declare module '@ctrl/tinycolor/dist/module/format-input' { - export * from '@ctrl/tinycolor/dist/format-input'; -} - -declare module '@ctrl/tinycolor/dist/module/util' { - export * from '@ctrl/tinycolor/dist/util'; -}