Skip to content

Commit 45d5526

Browse files
authored
Updated color-utils to use TypeScript (#481)
* Ran yarn build with latest babel env * Ran build without terser to check differences * Added TypeScript config * Restored terser * Added type definitions * Removed unused dependencies
1 parent 430b2c9 commit 45d5526

File tree

12 files changed

+118
-137
lines changed

12 files changed

+118
-137
lines changed

packages/color-utils/.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
module.exports = {
33
plugins: ['ghost'],
44
extends: [
5-
'plugin:ghost/es'
5+
'plugin:ghost/ts'
66
]
77
};

packages/color-utils/cjs/color-utils.js

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,54 +7,37 @@ var Color = require('color');
77
function lightenToContrastThreshold(foreground, background, contrastThreshold) {
88
const foregroundColor = Color(foreground);
99
const backgroundColor = Color(background);
10-
11-
const {h,s} = foregroundColor.hsl().object();
12-
10+
const { h, s } = foregroundColor.hsl().object();
1311
let newColor = foregroundColor;
14-
1512
while (newColor.contrast(backgroundColor) < contrastThreshold) {
1613
if (newColor.lightness() >= 100) {
1714
break;
1815
}
19-
20-
newColor = Color({h, s, l: newColor.lightness() + 5});
16+
newColor = Color({ h, s, l: newColor.lightness() + 5 });
2117
}
22-
2318
return newColor;
2419
}
25-
2620
function darkenToContrastThreshold(foreground, background, contrastThreshold) {
2721
const foregroundColor = Color(foreground);
2822
const backgroundColor = Color(background);
29-
30-
const {h,s} = foregroundColor.hsl().object();
31-
23+
const { h, s } = foregroundColor.hsl().object();
3224
let newColor = foregroundColor;
33-
3425
while (newColor.contrast(backgroundColor) < contrastThreshold) {
3526
if (newColor.lightness() <= 0) {
3627
break;
3728
}
38-
39-
newColor = Color({h, s, l: newColor.lightness() - 5});
29+
newColor = Color({ h, s, l: newColor.lightness() - 5 });
4030
}
41-
4231
return newColor;
4332
}
44-
4533
function textColorForBackgroundColor(background) {
4634
const backgroundColor = Color(background);
47-
48-
const white = Color({r: 255, g: 255, b: 255});
49-
const black = Color({r: 0, g: 0, b: 0});
50-
35+
const white = Color({ r: 255, g: 255, b: 255 });
36+
const black = Color({ r: 0, g: 0, b: 0 });
5137
// shared with Portal https://github.com/TryGhost/Portal/blob/317876f20d22431df15e655ea6cc197fe636615e/src/utils/contrast-color.js#L26-L29
52-
const yiq = (
53-
backgroundColor.red() * 0.299 +
38+
const yiq = (backgroundColor.red() * 0.299 +
5439
backgroundColor.green() * 0.587 +
55-
backgroundColor.b() * 0.114
56-
);
57-
40+
backgroundColor.b() * 0.114);
5841
return (yiq >= 186) ? black : white;
5942
}
6043

packages/color-utils/es/color-utils.js

Lines changed: 50 additions & 70 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/color-utils/es/color-utils.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/color-utils/package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
"umd:main": "umd/color-utils.min.js",
99
"unpkg": "umd/color-utils.min.js",
1010
"module": "es/color-utils.js",
11-
"source": "lib/color-utils.js",
11+
"source": "src/color-utils.ts",
1212
"scripts": {
1313
"dev": "echo \"Implement me!\"",
1414
"test": "NODE_ENV=testing mocha './test/**/*.test.js'",
15-
"build": "rollup -c",
15+
"build": "rollup -c && tsc --declaration --emitDeclarationOnly --declarationDir ./types",
1616
"lint": "eslint . --ext .js --cache",
1717
"prepare": "NODE_ENV=production yarn build",
1818
"posttest": "yarn lint"
@@ -21,25 +21,27 @@
2121
"LICENSE",
2222
"README.md",
2323
"cjs/",
24-
"lib/",
24+
"src/",
2525
"umd/",
2626
"es/"
2727
],
2828
"publishConfig": {
2929
"access": "public"
3030
},
3131
"devDependencies": {
32+
"@rollup/plugin-typescript": "11.1.5",
3233
"mocha": "10.2.0",
3334
"rollup": "2.79.1",
34-
"rollup-plugin-babel": "4.4.0",
3535
"rollup-plugin-commonjs": "10.1.0",
3636
"rollup-plugin-node-resolve": "5.2.0",
3737
"rollup-plugin-replace": "2.2.0",
3838
"rollup-plugin-terser": "7.0.2",
3939
"should": "13.2.3",
40-
"sinon": "17.0.0"
40+
"sinon": "17.0.0",
41+
"typescript": "5.2.2"
4142
},
4243
"dependencies": {
44+
"@types/color": "3.0.2",
4345
"color": "^3.2.1"
4446
}
4547
}

0 commit comments

Comments
 (0)