@fils/color 0.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.
@@ -0,0 +1,3 @@
1
+ export declare function drawColorWheel(canvas: HTMLCanvasElement): void;
2
+ export declare function drawColorPickerBar(canvas: HTMLCanvasElement, x?: number, y?: number, width?: number, height?: number): void;
3
+ export declare function drawColorPickerSL(canvas: HTMLCanvasElement, angle: number, x?: number, y?: number, width?: number, height?: number): void;
@@ -0,0 +1,49 @@
1
+ import { hsbToHex } from "./utils";
2
+ export function drawColorWheel(canvas) {
3
+ const centerX = canvas.width / 2;
4
+ const centerY = canvas.height / 2;
5
+ const radius = Math.min(canvas.width, canvas.height) / 2;
6
+ const ctx = canvas.getContext('2d');
7
+ for (let i = 0; i < 360; i++) {
8
+ const rad = (i * Math.PI) / 180;
9
+ const x = centerX + radius * Math.cos(rad);
10
+ const y = centerY + radius * Math.sin(rad);
11
+ ctx.beginPath();
12
+ ctx.arc(x, y, 5, 0, 2 * Math.PI);
13
+ ctx.fillStyle = `hsl(${i}, 100%, 50%)`;
14
+ ctx.fill();
15
+ }
16
+ }
17
+ export function drawColorPickerBar(canvas, x, y, width, height) {
18
+ const _x = x !== undefined ? x : 0;
19
+ const _y = y !== undefined ? y : 0;
20
+ const w = width !== undefined ? width : canvas.width;
21
+ const h = height !== undefined ? height : canvas.height;
22
+ const ctx = canvas.getContext('2d');
23
+ for (let i = 0; i < w; i++) {
24
+ const dx = _x + i;
25
+ const angle = 360 * i / w;
26
+ ctx.fillStyle = `hsl(${angle}, 100%, 50%)`;
27
+ ctx.fillRect(dx, _y, 1, h);
28
+ }
29
+ }
30
+ export function drawColorPickerSL(canvas, angle, x, y, width, height) {
31
+ const _x = x !== undefined ? x : 0;
32
+ const _y = y !== undefined ? y : 0;
33
+ const w = width !== undefined ? width : canvas.width;
34
+ const h = height !== undefined ? height : canvas.height;
35
+ const ctx = canvas.getContext('2d');
36
+ const sw = w / 100;
37
+ const sh = h / 100;
38
+ for (let i = 0; i <= 100; i++) {
39
+ for (let j = 0; j <= 100; j++) {
40
+ const hex = hsbToHex({
41
+ h: angle,
42
+ s: j,
43
+ b: i
44
+ });
45
+ ctx.fillStyle = hex;
46
+ ctx.fillRect(_x + w * j / 100, _y + h - h * i / 100, sw, sh);
47
+ }
48
+ }
49
+ }
package/lib/main.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './utils';
2
+ export * from './canvas-utils';
package/lib/main.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from './utils';
2
+ export * from './canvas-utils';
package/lib/utils.d.ts ADDED
@@ -0,0 +1,26 @@
1
+ export type RGBColor = {
2
+ r: number;
3
+ g: number;
4
+ b: number;
5
+ };
6
+ export type HSBColor = {
7
+ h: number;
8
+ s: number;
9
+ b: number;
10
+ };
11
+ export type HSLColor = {
12
+ h: number;
13
+ s: number;
14
+ l: number;
15
+ };
16
+ export declare function componentToHex(c: number): string;
17
+ export declare function rgbToHex(color: RGBColor): string;
18
+ export declare function hexToRgb(hex: string): RGBColor;
19
+ export declare function rgbToHsl(color: RGBColor): HSLColor;
20
+ export declare function hslToRgb(color: HSLColor): RGBColor;
21
+ export declare function hslToHex(color: HSLColor): string;
22
+ export declare function rgbToHsb(color: RGBColor): HSBColor;
23
+ export declare function hsbToRgb(color: HSBColor): RGBColor;
24
+ export declare function hsbToHex(color: HSBColor): string;
25
+ export declare function rgbToString(color: RGBColor): string;
26
+ export declare function hsbToString(color: HSBColor): string;
package/lib/utils.js ADDED
@@ -0,0 +1,129 @@
1
+ export function componentToHex(c) {
2
+ const hex = c.toString(16);
3
+ return hex.length === 1 ? '0' + hex : hex;
4
+ }
5
+ export function rgbToHex(color) {
6
+ return '#' + componentToHex(color.r) + componentToHex(color.g) + componentToHex(color.b);
7
+ }
8
+ export function hexToRgb(hex) {
9
+ const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
10
+ return result ? {
11
+ r: parseInt(result[1], 16),
12
+ g: parseInt(result[2], 16),
13
+ b: parseInt(result[3], 16)
14
+ } : { r: 0, g: 0, b: 0 };
15
+ }
16
+ export function rgbToHsl(color) {
17
+ const r = color.r / 255, g = color.g / 255, b = color.b / 255;
18
+ const max = Math.max(r, g, b);
19
+ const min = Math.min(r, g, b);
20
+ let h, s, l = (max + min) / 2;
21
+ if (max == min) {
22
+ h = s = 0;
23
+ }
24
+ else {
25
+ let d = max - min;
26
+ s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
27
+ switch (max) {
28
+ case r:
29
+ h = (g - b) / d + (g < b ? 6 : 0);
30
+ break;
31
+ case g:
32
+ h = (b - r) / d + 2;
33
+ break;
34
+ case b:
35
+ h = (r - g) / d + 4;
36
+ break;
37
+ }
38
+ h /= 6;
39
+ }
40
+ return {
41
+ h: h,
42
+ s: s,
43
+ l: l
44
+ };
45
+ }
46
+ function hue2rgb(p, q, t) {
47
+ if (t < 0)
48
+ t += 1;
49
+ if (t > 1)
50
+ t -= 1;
51
+ if (t < 1 / 6)
52
+ return p + (q - p) * 6 * t;
53
+ if (t < 1 / 2)
54
+ return q;
55
+ if (t < 2 / 3)
56
+ return p + (q - p) * (2 / 3 - t) * 6;
57
+ return p;
58
+ }
59
+ export function hslToRgb(color) {
60
+ let r, g, b;
61
+ const h = color.h, s = color.s, l = color.l;
62
+ if (s == 0) {
63
+ r = g = b = l;
64
+ }
65
+ else {
66
+ let q = color.l < 0.5 ? l * (1 + s) : l + s - l * s;
67
+ let p = 2 * l - q;
68
+ r = hue2rgb(p, q, h + 1 / 3);
69
+ g = hue2rgb(p, q, h);
70
+ b = hue2rgb(p, q, h - 1 / 3);
71
+ }
72
+ return {
73
+ r: Math.round(r * 255),
74
+ g: Math.round(g * 255),
75
+ b: Math.round(b * 255)
76
+ };
77
+ }
78
+ export function hslToHex(color) {
79
+ return rgbToHex(hslToRgb(color));
80
+ }
81
+ export function rgbToHsb(color) {
82
+ const r = color.r / 255;
83
+ const g = color.g / 255;
84
+ const b = color.b / 255;
85
+ const max = Math.max(r, g, b), min = Math.min(r, g, b);
86
+ let h = 0, s = 0, v = max;
87
+ const d = max - min;
88
+ s = max === 0 ? 0 : d / max;
89
+ if (max === min) {
90
+ h = 0;
91
+ }
92
+ else {
93
+ switch (max) {
94
+ case r:
95
+ h = (g - b) / d + (g < b ? 6 : 0);
96
+ break;
97
+ case g:
98
+ h = (b - r) / d + 2;
99
+ break;
100
+ case b:
101
+ h = (r - g) / d + 4;
102
+ break;
103
+ }
104
+ h /= 6;
105
+ }
106
+ return { h: h * 360, s: s * 100, b: v * 100 };
107
+ }
108
+ export function hsbToRgb(color) {
109
+ let h = color.h, s = color.s, b = color.b;
110
+ s /= 100;
111
+ b /= 100;
112
+ const k = (n) => (n + h / 60) % 6;
113
+ const f = (n) => b * (1 - s * Math.max(0, Math.min(k(n), 4 - k(n), 1)));
114
+ return {
115
+ r: Math.round(255 * f(5)),
116
+ g: Math.round(255 * f(3)),
117
+ b: Math.round(255 * f(1))
118
+ };
119
+ }
120
+ export function hsbToHex(color) {
121
+ const rgb = hsbToRgb(color);
122
+ return rgbToHex(rgb);
123
+ }
124
+ export function rgbToString(color) {
125
+ return `R: ${color.r}, G: ${color.g}, B: ${color.b}`;
126
+ }
127
+ export function hsbToString(color) {
128
+ return `H: ${color.h}, S: ${color.s}, B: ${color.b}`;
129
+ }
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@fils/color",
3
+ "version": "0.0.1",
4
+ "description": "Color Package written in TypeScript",
5
+ "main": "lib/main.js",
6
+ "repository": "git@github.com:fil-studio/fils.git",
7
+ "homepage": "https://github.com/fil-studio/fils/tree/main/packages/color",
8
+ "author": "fil studio <hello@fil.studio>",
9
+ "license": "Apache-2.0",
10
+ "private": false,
11
+ "scripts": {
12
+ "prepare": "yarn build",
13
+ "build": "tsc --build tsconfig.color.json"
14
+ },
15
+ "files": [
16
+ "lib",
17
+ "examples",
18
+ "README.md"
19
+ ],
20
+ "devDependencies": {
21
+ "typescript": "^4.8.4"
22
+ }
23
+ }