Swap from color to colord for color manipulation (#733)

This commit is contained in:
Jaco
2023-03-13 12:55:30 +02:00
committed by GitHub
parent e1f96f1fcf
commit c1b3cc87ee
6 changed files with 34 additions and 80 deletions
-1
View File
@@ -25,7 +25,6 @@
"@polkadot/ui-shared": "3.0.3-1-x", "@polkadot/ui-shared": "3.0.3-1-x",
"@polkadot/util": "^11.0.2", "@polkadot/util": "^11.0.2",
"@polkadot/util-crypto": "^11.0.2", "@polkadot/util-crypto": "^11.0.2",
"color": "^3.2.1",
"ethereum-blockies-base64": "^1.0.2", "ethereum-blockies-base64": "^1.0.2",
"jdenticon": "3.2.0", "jdenticon": "3.2.0",
"react-copy-to-clipboard": "^5.1.0", "react-copy-to-clipboard": "^5.1.0",
+1 -2
View File
@@ -20,13 +20,12 @@
"version": "3.0.3-1-x", "version": "3.0.3-1-x",
"main": "index.js", "main": "index.js",
"dependencies": { "dependencies": {
"color": "^3.2.1", "colord": "^2.9.3",
"tslib": "^2.5.0" "tslib": "^2.5.0"
}, },
"devDependencies": { "devDependencies": {
"@polkadot/util": "^11.0.2", "@polkadot/util": "^11.0.2",
"@polkadot/util-crypto": "^11.0.2", "@polkadot/util-crypto": "^11.0.2",
"@types/color": "^3.0.3",
"@types/xmlserializer": "^0.6.3", "@types/xmlserializer": "^0.6.3",
"xmlserializer": "^0.6.1" "xmlserializer": "^0.6.1"
}, },
@@ -19,8 +19,9 @@ describe('colors', (): void => {
expect( expect(
colors() colors()
).toEqual( ).toEqual(
// 'hsla(166.70000000000005, 98.6%, 27.6%, 0.9)' // with original color ...
'hsl(37.19999999999999, 100%, 54.9%)' // 'hsl(37.19999999999999, 100%, 54.9%)'
'hsl(37, 100%, 55%)'
); );
}); });
@@ -28,17 +29,22 @@ describe('colors', (): void => {
expect( expect(
colors(0.5) colors(0.5)
).toEqual( ).toEqual(
// 'hsla(166.70000000000005, 98.6%, 27.6%, 0.5)' // with original color ...
'hsla(37.19999999999999, 100%, 54.9%, 0.5)' // 'hsla(37.19999999999999, 100%, 54.9%, 0.5)'
'hsla(37, 100%, 55%, 0.5)'
); );
}); });
it('rolates colors', (): void => { it('rolates colors', (): void => {
colors(); const orig = colors();
expect(
orig
).toEqual('hsl(37, 100%, 55%)');
expect( expect(
colors() colors()
).not.toEqual('hsla(166.70000000000005, 98.6%, 27.6%, 0.9)'); ).not.toEqual(orig);
}); });
it('works in edge conditions (0xff)', (): void => { it('works in edge conditions (0xff)', (): void => {
@@ -53,8 +59,9 @@ describe('colors', (): void => {
expect( expect(
colors() colors()
).toEqual( ).toEqual(
// 'hsla(234.39999999999998, 75.9%, 51.2%, 0.9)' // with original color ...
'hsl(15, 0%, 100%)' // 'hsl(15, 0%, 100%)'
'hsl(0, 0%, 100%)'
); );
}); });
}); });
@@ -3,9 +3,10 @@
// Copyright 2016 Dan Finlay // Copyright 2016 Dan Finlay
import type { Colord } from 'colord';
import type { ColorGen, Seeder } from './types.js'; import type { ColorGen, Seeder } from './types.js';
import Color from 'color'; import { colord } from 'colord';
import { COLORS } from './defaults.js'; import { COLORS } from './defaults.js';
@@ -13,8 +14,8 @@ const WOBBLE = 30;
export function colors (seeder: Seeder): ColorGen { export function colors (seeder: Seeder): ColorGen {
const amount = (seeder() * WOBBLE) - (WOBBLE / 2); const amount = (seeder() * WOBBLE) - (WOBBLE / 2);
const all = COLORS.map((hex): Color => const all = COLORS.map((hex): Colord =>
Color(hex).rotate(amount) colord(hex).rotate(amount)
); );
return (alpha = 1): string => { return (alpha = 1): string => {
@@ -22,6 +23,6 @@ export function colors (seeder: Seeder): ColorGen {
return all.splice(index, 1)[0] return all.splice(index, 1)[0]
.alpha(alpha) .alpha(alpha)
.string(); .toHslString();
}; };
} }
@@ -15,7 +15,9 @@ describe('identicon', (): void => {
beachballIcon(new Uint8Array(32), { isAlternative: false, size: 256 }) as any beachballIcon(new Uint8Array(32), { isAlternative: false, size: 256 }) as any
) )
).toEqual( ).toEqual(
'<div xmlns="http://www.w3.org/1999/xhtml" class="" style="background: white; border-radius: 128px; display: inline-block; height: 256px; margin: 0px; overflow: hidden; padding: 0px; width: 256px;"><div class="" style="background: rgb(20, 20, 20); border-radius: 128px; display: inline-block; height: 256px; margin: 0px; overflow: hidden; padding: 0px; width: 256px;"><svg xmlns="http://www.w3.org/2000/svg" x="0" y="0" width="256" height="256"><circle cx="128" cy="140.8" r="128" fill="hsl(212.10000000000002, 65.6%, 55.5%)"/><circle cx="128" cy="153.6" r="102.4" fill="hsl(9.800000000000011, 90.7%, 57.6%)"/><circle cx="128" cy="166.4" r="76.8" fill="hsl(345, 85.7%, 86.3%)"/><circle cx="128" cy="179.2" r="51.2" fill="hsl(261.9, 100%, 87.3%)"/><circle cx="128" cy="192" r="25.6" fill="hsl(345, 100%, 25.1%)"/></svg></div></div>' // with original color ...
// '<div xmlns="http://www.w3.org/1999/xhtml" class="" style="background: white; border-radius: 128px; display: inline-block; height: 256px; margin: 0px; overflow: hidden; padding: 0px; width: 256px;"><div class="" style="background: rgb(20, 20, 20); border-radius: 128px; display: inline-block; height: 256px; margin: 0px; overflow: hidden; padding: 0px; width: 256px;"><svg xmlns="http://www.w3.org/2000/svg" x="0" y="0" width="256" height="256"><circle cx="128" cy="140.8" r="128" fill="hsl(212.10000000000002, 65.6%, 55.5%)"/><circle cx="128" cy="153.6" r="102.4" fill="hsl(9.800000000000011, 90.7%, 57.6%)"/><circle cx="128" cy="166.4" r="76.8" fill="hsl(345, 85.7%, 86.3%)"/><circle cx="128" cy="179.2" r="51.2" fill="hsl(261.9, 100%, 87.3%)"/><circle cx="128" cy="192" r="25.6" fill="hsl(345, 100%, 25.1%)"/></svg></div></div>'
'<div xmlns="http://www.w3.org/1999/xhtml" class="" style="background: white; border-radius: 128px; display: inline-block; height: 256px; margin: 0px; overflow: hidden; padding: 0px; width: 256px;"><div class="" style="background: rgb(26, 26, 26); border-radius: 128px; display: inline-block; height: 256px; margin: 0px; overflow: hidden; padding: 0px; width: 256px;"><svg xmlns="http://www.w3.org/2000/svg" x="0" y="0" width="256" height="256"><circle cx="128" cy="140.8" r="128" fill="hsl(212, 66%, 55%)"/><circle cx="128" cy="153.6" r="102.4" fill="hsl(10, 91%, 58%)"/><circle cx="128" cy="166.4" r="76.8" fill="hsl(345, 86%, 86%)"/><circle cx="128" cy="179.2" r="51.2" fill="hsl(262, 100%, 87%)"/><circle cx="128" cy="192" r="25.6" fill="hsl(345, 100%, 25%)"/></svg></div></div>'
); );
}); });
@@ -26,7 +28,9 @@ describe('identicon', (): void => {
beachballIcon(new Uint8Array(32), { isAlternative: false, size: 100 }, 'testClass', { display: 'block' }) as any beachballIcon(new Uint8Array(32), { isAlternative: false, size: 100 }, 'testClass', { display: 'block' }) as any
) )
).toEqual( ).toEqual(
'<div xmlns="http://www.w3.org/1999/xhtml" class="testClass" style="background: white; border-radius: 50px; display: block; height: 100px; margin: 0px; overflow: hidden; padding: 0px; width: 100px;"><div class="" style="background: rgb(20, 20, 20); border-radius: 50px; display: inline-block; height: 100px; margin: 0px; overflow: hidden; padding: 0px; width: 100px;"><svg xmlns="http://www.w3.org/2000/svg" x="0" y="0" width="100" height="100"><circle cx="50" cy="55" r="50" fill="hsl(212.10000000000002, 65.6%, 55.5%)"/><circle cx="50" cy="60" r="40" fill="hsl(9.800000000000011, 90.7%, 57.6%)"/><circle cx="50" cy="65" r="30" fill="hsl(345, 85.7%, 86.3%)"/><circle cx="50" cy="70" r="20" fill="hsl(261.9, 100%, 87.3%)"/><circle cx="50" cy="75" r="10" fill="hsl(345, 100%, 25.1%)"/></svg></div></div>' // with original color ...
// '<div xmlns="http://www.w3.org/1999/xhtml" class="testClass" style="background: white; border-radius: 50px; display: block; height: 100px; margin: 0px; overflow: hidden; padding: 0px; width: 100px;"><div class="" style="background: rgb(20, 20, 20); border-radius: 50px; display: inline-block; height: 100px; margin: 0px; overflow: hidden; padding: 0px; width: 100px;"><svg xmlns="http://www.w3.org/2000/svg" x="0" y="0" width="100" height="100"><circle cx="50" cy="55" r="50" fill="hsl(212.10000000000002, 65.6%, 55.5%)"/><circle cx="50" cy="60" r="40" fill="hsl(9.800000000000011, 90.7%, 57.6%)"/><circle cx="50" cy="65" r="30" fill="hsl(345, 85.7%, 86.3%)"/><circle cx="50" cy="70" r="20" fill="hsl(261.9, 100%, 87.3%)"/><circle cx="50" cy="75" r="10" fill="hsl(345, 100%, 25.1%)"/></svg></div></div>'
'<div xmlns="http://www.w3.org/1999/xhtml" class="testClass" style="background: white; border-radius: 50px; display: block; height: 100px; margin: 0px; overflow: hidden; padding: 0px; width: 100px;"><div class="" style="background: rgb(26, 26, 26); border-radius: 50px; display: inline-block; height: 100px; margin: 0px; overflow: hidden; padding: 0px; width: 100px;"><svg xmlns="http://www.w3.org/2000/svg" x="0" y="0" width="100" height="100"><circle cx="50" cy="55" r="50" fill="hsl(212, 66%, 55%)"/><circle cx="50" cy="60" r="40" fill="hsl(10, 91%, 58%)"/><circle cx="50" cy="65" r="30" fill="hsl(345, 86%, 86%)"/><circle cx="50" cy="70" r="20" fill="hsl(262, 100%, 87%)"/><circle cx="50" cy="75" r="10" fill="hsl(345, 100%, 25%)"/></svg></div></div>'
); );
}); });
}); });
+7 -63
View File
@@ -1523,7 +1523,6 @@ __metadata:
"@types/react-copy-to-clipboard": ^5.0.4 "@types/react-copy-to-clipboard": ^5.0.4
"@types/react-dom": ^18.0.11 "@types/react-dom": ^18.0.11
"@types/styled-components": ^5.1.26 "@types/styled-components": ^5.1.26
color: ^3.2.1
ethereum-blockies-base64: ^1.0.2 ethereum-blockies-base64: ^1.0.2
jdenticon: 3.2.0 jdenticon: 3.2.0
react-copy-to-clipboard: ^5.1.0 react-copy-to-clipboard: ^5.1.0
@@ -1624,9 +1623,8 @@ __metadata:
dependencies: dependencies:
"@polkadot/util": ^11.0.2 "@polkadot/util": ^11.0.2
"@polkadot/util-crypto": ^11.0.2 "@polkadot/util-crypto": ^11.0.2
"@types/color": ^3.0.3
"@types/xmlserializer": ^0.6.3 "@types/xmlserializer": ^0.6.3
color: ^3.2.1 colord: ^2.9.3
tslib: ^2.5.0 tslib: ^2.5.0
xmlserializer: ^0.6.1 xmlserializer: ^0.6.1
peerDependencies: peerDependencies:
@@ -2270,31 +2268,6 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"@types/color-convert@npm:*":
version: 2.0.0
resolution: "@types/color-convert@npm:2.0.0"
dependencies:
"@types/color-name": "*"
checksum: 027b68665dc2278cc2d83e796ada0a05a08aa5a11297e227c48c7f9f6eac518dec98578ab0072bd211963d3e4b431da70b20ea28d6c3136d0badfd3f9913baee
languageName: node
linkType: hard
"@types/color-name@npm:*":
version: 1.1.1
resolution: "@types/color-name@npm:1.1.1"
checksum: b71fcad728cc68abcba1d405742134410c8f8eb3c2ef18113b047afca158ad23a4f2c229bcf71a38f4a818dead375c45b20db121d0e69259c2d81e97a740daa6
languageName: node
linkType: hard
"@types/color@npm:^3.0.3":
version: 3.0.3
resolution: "@types/color@npm:3.0.3"
dependencies:
"@types/color-convert": "*"
checksum: 4a54a6eca687b1f19d393c6c44e292009e913fa2377b5b815d1cd04470131087d7a799718d9e66a970f43e6ba16e7056a5b40b49d2d0d1d0f1530aff23864528
languageName: node
linkType: hard
"@types/connect-history-api-fallback@npm:^1.3.5": "@types/connect-history-api-fallback@npm:^1.3.5":
version: 1.3.5 version: 1.3.5
resolution: "@types/connect-history-api-fallback@npm:1.3.5" resolution: "@types/connect-history-api-fallback@npm:1.3.5"
@@ -4350,7 +4323,7 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"color-convert@npm:^1.9.0, color-convert@npm:^1.9.3": "color-convert@npm:^1.9.0":
version: 1.9.3 version: 1.9.3
resolution: "color-convert@npm:1.9.3" resolution: "color-convert@npm:1.9.3"
dependencies: dependencies:
@@ -4375,23 +4348,13 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"color-name@npm:^1.0.0, color-name@npm:^1.1.4, color-name@npm:~1.1.4": "color-name@npm:^1.1.4, color-name@npm:~1.1.4":
version: 1.1.4 version: 1.1.4
resolution: "color-name@npm:1.1.4" resolution: "color-name@npm:1.1.4"
checksum: b0445859521eb4021cd0fb0cc1a75cecf67fceecae89b63f62b201cca8d345baf8b952c966862a9d9a2632987d4f6581f0ec8d957dfacece86f0a7919316f610 checksum: b0445859521eb4021cd0fb0cc1a75cecf67fceecae89b63f62b201cca8d345baf8b952c966862a9d9a2632987d4f6581f0ec8d957dfacece86f0a7919316f610
languageName: node languageName: node
linkType: hard linkType: hard
"color-string@npm:^1.6.0":
version: 1.6.0
resolution: "color-string@npm:1.6.0"
dependencies:
color-name: ^1.0.0
simple-swizzle: ^0.2.2
checksum: 33466a65277dd3d4ce24ef1991b47069292f75d1a43b0d2e7ea43076ba793728e965d50deed2b523f35519f4995a908253fcbcc774baceae8e439bc78c02e850
languageName: node
linkType: hard
"color-support@npm:^1.1.3": "color-support@npm:^1.1.3":
version: 1.1.3 version: 1.1.3
resolution: "color-support@npm:1.1.3" resolution: "color-support@npm:1.1.3"
@@ -4401,13 +4364,10 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"color@npm:^3.2.1": "colord@npm:^2.9.3":
version: 3.2.1 version: 2.9.3
resolution: "color@npm:3.2.1" resolution: "colord@npm:2.9.3"
dependencies: checksum: 95d909bfbcfd8d5605cbb5af56f2d1ce2b323990258fd7c0d2eb0e6d3bb177254d7fb8213758db56bb4ede708964f78c6b992b326615f81a18a6aaf11d64c650
color-convert: ^1.9.3
color-string: ^1.6.0
checksum: f81220e8b774d35865c2561be921f5652117638dcda7ca4029262046e37fc2444ac7bbfdd110cf1fd9c074a4ee5eda8f85944ffbdda26186b602dd9bb05f6400
languageName: node languageName: node
linkType: hard linkType: hard
@@ -7481,13 +7441,6 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"is-arrayish@npm:^0.3.1":
version: 0.3.2
resolution: "is-arrayish@npm:0.3.2"
checksum: 977e64f54d91c8f169b59afcd80ff19227e9f5c791fa28fa2e5bce355cbaf6c2c356711b734656e80c9dd4a854dd7efcf7894402f1031dfc5de5d620775b4d5f
languageName: node
linkType: hard
"is-bigint@npm:^1.0.1": "is-bigint@npm:^1.0.1":
version: 1.0.1 version: 1.0.1
resolution: "is-bigint@npm:1.0.1" resolution: "is-bigint@npm:1.0.1"
@@ -11562,15 +11515,6 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"simple-swizzle@npm:^0.2.2":
version: 0.2.2
resolution: "simple-swizzle@npm:0.2.2"
dependencies:
is-arrayish: ^0.3.1
checksum: a7f3f2ab5c76c4472d5c578df892e857323e452d9f392e1b5cf74b74db66e6294a1e1b8b390b519fa1b96b5b613f2a37db6cffef52c3f1f8f3c5ea64eb2d54c0
languageName: node
linkType: hard
"sisteransi@npm:^1.0.5": "sisteransi@npm:^1.0.5":
version: 1.0.5 version: 1.0.5
resolution: "sisteransi@npm:1.0.5" resolution: "sisteransi@npm:1.0.5"