@livelink/swatchster 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/settings.json +3 -0
- package/CODE_OF_CONDUCT.md +74 -0
- package/LICENSE +21 -0
- package/README.md +9 -0
- package/index.js +9 -0
- package/package.json +22 -0
- package/src/helpers/colour-distance.js +15 -0
- package/src/helpers/hex-to-rgb.js +9 -0
- package/src/helpers/rgb-to-hex.js +9 -0
- package/src/swatch-from-canvas.js +38 -0
- package/src/swatch-from-hex.js +6 -0
- package/src/swatch-from-rgb.js +24 -0
- package/tests/defaults/swatch-from-canvas.test.js +54 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
|
10
|
+
orientation.
|
|
11
|
+
|
|
12
|
+
## Our Standards
|
|
13
|
+
|
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
|
15
|
+
include:
|
|
16
|
+
|
|
17
|
+
* Using welcoming and inclusive language
|
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
|
19
|
+
* Gracefully accepting constructive criticism
|
|
20
|
+
* Focusing on what is best for the community
|
|
21
|
+
* Showing empathy towards other community members
|
|
22
|
+
|
|
23
|
+
Examples of unacceptable behavior by participants include:
|
|
24
|
+
|
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
|
26
|
+
advances
|
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
|
28
|
+
* Public or private harassment
|
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
|
30
|
+
address, without explicit permission
|
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
32
|
+
professional setting
|
|
33
|
+
|
|
34
|
+
## Our Responsibilities
|
|
35
|
+
|
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
|
38
|
+
response to any instances of unacceptable behavior.
|
|
39
|
+
|
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
|
44
|
+
threatening, offensive, or harmful.
|
|
45
|
+
|
|
46
|
+
## Scope
|
|
47
|
+
|
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
|
49
|
+
when an individual is representing the project or its community. Examples of
|
|
50
|
+
representing a project or community include using an official project e-mail
|
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
|
53
|
+
further defined and clarified by project maintainers.
|
|
54
|
+
|
|
55
|
+
## Enforcement
|
|
56
|
+
|
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
58
|
+
reported by contacting the project team at jamie@thelucid.com. All
|
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
|
63
|
+
|
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
|
66
|
+
members of the project's leadership.
|
|
67
|
+
|
|
68
|
+
## Attribution
|
|
69
|
+
|
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
|
72
|
+
|
|
73
|
+
[homepage]: https://contributor-covenant.org
|
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Livelink Technology Ltd
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@livelink/swatchster",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "A versatile colour swatch package that extracts colour hex codes from designs.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"repository": "https://github.com/livelink/swatchster",
|
|
7
|
+
"author": "Emma Bovill <e.bovill@livelinktechonology.net>",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"private": false,
|
|
10
|
+
"type": "module",
|
|
11
|
+
"scripts": {
|
|
12
|
+
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --verbose"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"canvas": "^2.11.2",
|
|
16
|
+
"jest": "^29.7.0"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
20
|
+
"jsdom": "^22.1.0"
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
|
|
2
|
+
export const colourDistance = (hexColor1, hexColor2) => {
|
|
3
|
+
const r1 = parseInt(hexColor1.slice(1, 3), 16);
|
|
4
|
+
const g1 = parseInt(hexColor1.slice(3, 5), 16);
|
|
5
|
+
const b1 = parseInt(hexColor1.slice(5, 7), 16);
|
|
6
|
+
const r2 = parseInt(hexColor2.slice(1, 3), 16);
|
|
7
|
+
const g2 = parseInt(hexColor2.slice(3, 5), 16);
|
|
8
|
+
const b2 = parseInt(hexColor2.slice(5, 7), 16);
|
|
9
|
+
|
|
10
|
+
const dr = r1 - r2;
|
|
11
|
+
const dg = g1 - g2;
|
|
12
|
+
const db = b1 - b2;
|
|
13
|
+
|
|
14
|
+
return Math.sqrt(dr * dr + dg * dg + db * db);
|
|
15
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// source: https://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb
|
|
2
|
+
export const hexToRgb = (hex) => {
|
|
3
|
+
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
|
|
4
|
+
return result ? [
|
|
5
|
+
parseInt(result[1], 16),
|
|
6
|
+
parseInt(result[2], 16),
|
|
7
|
+
parseInt(result[3], 16)
|
|
8
|
+
] : null;
|
|
9
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// source: https://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb
|
|
2
|
+
const componentToHex = (c) => {
|
|
3
|
+
var hex = c.toString(16);
|
|
4
|
+
return hex.length == 1 ? "0" + hex : hex;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export const rgbToHex = (r, g, b) => {
|
|
8
|
+
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
|
|
9
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { rgbToHex } from "./helpers/rgb-to-hex";
|
|
2
|
+
import { colourDistance } from "./helpers/colour-distance";
|
|
3
|
+
|
|
4
|
+
const swatchFromCanvas = (canvas, tolerance = 32) => {
|
|
5
|
+
const context = canvas.getContext('2d');
|
|
6
|
+
const imageData = context.getImageData(0, 0, canvas.width, canvas.height);
|
|
7
|
+
const pixelData = imageData.data;
|
|
8
|
+
const standoutColours = new Map();
|
|
9
|
+
|
|
10
|
+
for (let i = 0; i < pixelData.length; i += 4) {
|
|
11
|
+
const r = pixelData[i];
|
|
12
|
+
const g = pixelData[i + 1];
|
|
13
|
+
const b = pixelData[i + 2];
|
|
14
|
+
const a = pixelData[i + 3];
|
|
15
|
+
|
|
16
|
+
// Check if the pixel is not transparent
|
|
17
|
+
if (a > 0) {
|
|
18
|
+
const hexColour = rgbToHex(r, g, b);
|
|
19
|
+
|
|
20
|
+
// Check if this colour is not too similar to existing standout colours
|
|
21
|
+
let isStandout = true;
|
|
22
|
+
for (const [existingColour, _] of standoutColours.entries()) {
|
|
23
|
+
if (colourDistance(hexColour, existingColour) < tolerance) {
|
|
24
|
+
isStandout = false;
|
|
25
|
+
break;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (isStandout) {
|
|
30
|
+
standoutColours.set(hexColour, true);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return Array.from(standoutColours.keys());
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export { swatchFromCanvas };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { rgbToHex } from "./helpers/rgb-to-hex";
|
|
2
|
+
|
|
3
|
+
const SHADES = 10
|
|
4
|
+
|
|
5
|
+
export const swatchFromRgb = (red, green, blue) => {
|
|
6
|
+
const shade = rgbToHex(red, green, blue);
|
|
7
|
+
|
|
8
|
+
const redDiff = 255 - red;
|
|
9
|
+
const greenDiff = 255 - green;
|
|
10
|
+
const blueDiff = 255 - blue;
|
|
11
|
+
|
|
12
|
+
const redStepValue = redDiff / SHADES
|
|
13
|
+
const greenStepValue = greenDiff / SHADES
|
|
14
|
+
const blueStepValue = blueDiff / SHADES
|
|
15
|
+
|
|
16
|
+
const shade1 = [red - redStepValue * 2, green - greenStepValue * 2, blue - blueStepValue * 2];
|
|
17
|
+
const shade2 = [red - redStepValue, green - greenStepValue, blue - blueStepValue];
|
|
18
|
+
const shade3 = [red + redStepValue, green + greenStepValue, blue + blueStepValue];
|
|
19
|
+
const shade4 = [red + redStepValue * 2, green + greenStepValue * 2, blue + blueStepValue * 2];
|
|
20
|
+
|
|
21
|
+
const swatch = [rgbToHex(...shade1), rgbToHex(...shade2), shade, rgbToHex(...shade3), rgbToHex(...shade4)];
|
|
22
|
+
|
|
23
|
+
return swatch;
|
|
24
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jest-environment jsdom
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
// Import necessary functions and modules
|
|
6
|
+
import { swatchFromCanvas } from '../../src/swatch-from-canvas';
|
|
7
|
+
|
|
8
|
+
test('use jsdom in this test file', () => {
|
|
9
|
+
const element = document.createElement('div');
|
|
10
|
+
expect(element).not.toBeNull();
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
// Mock canvas and context for testing
|
|
14
|
+
const canvas_1 = document.createElement('canvas');
|
|
15
|
+
const ctx = canvas_1.getContext('2d');
|
|
16
|
+
|
|
17
|
+
// Sample canvas pixel data (replace with your own test data)
|
|
18
|
+
const imageData = ctx.createImageData(2, 2);
|
|
19
|
+
imageData.data.set([
|
|
20
|
+
255, 0, 0, 255, // Red
|
|
21
|
+
0, 255, 0, 255, // Green
|
|
22
|
+
0, 0, 255, 255, // Blue
|
|
23
|
+
255, 255, 0, 255, // Yellow
|
|
24
|
+
]);
|
|
25
|
+
|
|
26
|
+
// Set the canvas width and height
|
|
27
|
+
canvas_1.width = 2;
|
|
28
|
+
canvas_1.height = 2;
|
|
29
|
+
|
|
30
|
+
// Mock getImageData method
|
|
31
|
+
ctx.getImageData = () => imageData;
|
|
32
|
+
|
|
33
|
+
describe('swatchFromCanvas', () => {
|
|
34
|
+
it('should return standout colors from the canvas', () => {
|
|
35
|
+
const standoutColors = swatchFromCanvas(canvas_1);
|
|
36
|
+
|
|
37
|
+
// The expected result based on the sample data
|
|
38
|
+
const expectedColors = ['#ff0000', '#00ff00', '#0000ff', '#ffff00'];
|
|
39
|
+
|
|
40
|
+
// Assert that the function returns the expected standout colors
|
|
41
|
+
expect(standoutColors).toEqual(expectedColors);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('should return standout colors with custom tolerance', () => {
|
|
45
|
+
const standoutColors = swatchFromCanvas(canvas_1, 50);
|
|
46
|
+
|
|
47
|
+
// In this case, the colors are all standout due to higher tolerance
|
|
48
|
+
const expectedColors = ['#ff0000', '#00ff00', '#0000ff', '#ffff00'];
|
|
49
|
+
|
|
50
|
+
expect(standoutColors).toEqual(expectedColors);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Add more test cases as needed
|
|
54
|
+
});
|