Skip to content

Commit 9ff9846

Browse files
authored
fix: incorrect toHex value (#236)
* add failing test * add other scenario * fix to hex * copy readme * bump versions
1 parent a31d280 commit 9ff9846

File tree

5 files changed

+21
-3
lines changed

5 files changed

+21
-3
lines changed

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "color2k",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"private": true,
55
"description": "a color parsing and manipulation lib served in roughly 2kB",
66
"repository": {

scripts/build.ts

+8
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ async function main() {
6969
JSON.stringify(publishPackageJson, null, 2)
7070
);
7171

72+
const readme = await fs.promises.readFile(
73+
path.resolve(__dirname, '../README.md')
74+
);
75+
await fs.promises.writeFile(
76+
path.resolve(__dirname, '../dist/README.md'),
77+
readme
78+
);
79+
7280
console.log('DONE!');
7381
}
7482

src/toHex.test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import toHex from './toHex';
2+
import darken from './darken';
23

34
it("takes in any color and returns it's hex code", () => {
45
expect(toHex('palevioletred')).toMatchInlineSnapshot(`"#db7093"`);
@@ -15,3 +16,12 @@ test('rgba no transparency', () => {
1516
test('hsla w/ transparency', () => {
1617
expect(toHex('rgba(0, 255, 0, 0.5)')).toMatchInlineSnapshot(`"#00ff0080"`);
1718
});
19+
20+
test('https://github.com/ricokahler/color2k/issues/235', () => {
21+
expect(darken('#b00020', 0.2)).toMatchInlineSnapshot(
22+
`"hsla(349, 100%, 15%, 1)"`
23+
);
24+
expect(toHex(darken('#b00020', 0.2))).toMatchInlineSnapshot(`"#4d000e"`);
25+
26+
expect(toHex('#03dac5')).toMatchInlineSnapshot(`"#03dac5"`);
27+
});

src/toHex.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import guard from './guard';
66
*/
77
function toHex(color: string): string {
88
const [r, g, b, a] = parseToRgba(color);
9-
const hex = (x: number) => guard(0, 255, x).toString(16).padEnd(2, '0');
9+
const hex = (x: number) => guard(0, 255, x).toString(16).padStart(2, '0');
1010
return `#${hex(r)}${hex(g)}${hex(b)}${a < 1 ? hex(Math.round(a * 255)) : ''}`;
1111
}
1212

0 commit comments

Comments
 (0)