aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/ExtractColors.ts
blob: f6928c52af85926b7377fa9171ff88f598b0fcfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { extractColors } from 'extract-colors';
import { FinalColor } from 'extract-colors/lib/types/Color';

// Manages image color extraction
export class ExtractColors {
    // loads all images into img elements
    static loadImages = async (imageFiles: File[]): Promise<HTMLImageElement[]> => {
        try {
            const imageElements = await Promise.all(imageFiles.map(file => this.loadImage(file)));
            return imageElements;
        } catch (error) {
            console.error(error);
            return [];
        }
    };

    // loads a single img into an img element
    static loadImage = (file: File): Promise<HTMLImageElement> => {
        return new Promise((resolve, reject) => {
            const img = new Image();

            img.onload = () => resolve(img);
            img.onerror = error => reject(error);

            const url = URL.createObjectURL(file);
            img.src = url;
        });
    };

    // loads all images into img elements
    static loadImagesUrl = async (imageUrls: string[]): Promise<HTMLImageElement[]> => {
        try {
            const imageElements = await Promise.all(imageUrls.map(url => this.loadImageUrl(url)));
            return imageElements;
        } catch (error) {
            console.error(error);
            return [];
        }
    };

    // loads a single img into an img element
    static loadImageUrl = (url: string): Promise<HTMLImageElement> => {
        return new Promise((resolve, reject) => {
            const img = new Image();

            img.onload = () => resolve(img);
            img.onerror = error => reject(error);

            img.src = url;
        });
    };

    // extracts a list of collors from an img element
    static getImgColors = async (img: HTMLImageElement) => {
        const colors = await extractColors(img, { distance: 0.35 });
        return colors;
    };

    static simpleSort = (colors: FinalColor[]): FinalColor[] => {
        colors.sort((a, b) => {
            if (a.hue !== b.hue) {
                return b.hue - a.hue;
            } else {
                return b.saturation - a.saturation;
            }
        });
        return colors;
    };

    static sortColors(colors: FinalColor[]): FinalColor[] {
        // Convert color from RGB to CIELAB format
        const convertToLab = (color: FinalColor): number[] => {
            const r = color.red / 255;
            const g = color.green / 255;
            const b = color.blue / 255;

            const x = r * 0.4124564 + g * 0.3575761 + b * 0.1804375;
            const y = r * 0.2126729 + g * 0.7151522 + b * 0.072175;
            const z = r * 0.0193339 + g * 0.119192 + b * 0.9503041;

            const pivot = 0.008856;
            const factor = 903.3;

            const fx = x > pivot ? Math.cbrt(x) : (factor * x + 16) / 116;
            const fy = y > pivot ? Math.cbrt(y) : (factor * y + 16) / 116;
            const fz = z > pivot ? Math.cbrt(z) : (factor * z + 16) / 116;

            const L = 116 * fy - 16;
            const a = (fx - fy) * 500;
            const b1 = (fy - fz) * 200;

            return [L, a, b1];
        };

        // Sort colors using CIELAB distance for smooth transitions
        colors.sort((colorA, colorB) => {
            const labA = convertToLab(colorA);
            const labB = convertToLab(colorB);

            // Calculate Euclidean distance in CIELAB space
            const distanceA = Math.sqrt(Math.pow(labA[0] - labB[0], 2) + Math.pow(labA[1] - labB[1], 2) + Math.pow(labA[2] - labB[2], 2));

            const distanceB = Math.sqrt(Math.pow(labB[0] - labA[0], 2) + Math.pow(labB[1] - labA[1], 2) + Math.pow(labB[2] - labA[2], 2));

            return distanceA - distanceB; // Sort by CIELAB distance
        });

        return colors;
    }

    static hexToFinalColor = (hex: string): FinalColor => {
        const rgb = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);

        if (!rgb) {
            throw new Error('Invalid hex color format.');
        }

        const red = parseInt(rgb[1], 16);
        const green = parseInt(rgb[2], 16);
        const blue = parseInt(rgb[3], 16);

        const max = Math.max(red, green, blue);
        const min = Math.min(red, green, blue);
        const area = max - min;
        const intensity = (max + min) / 2;

        let hue = 0;
        let saturation = 0;
        let lightness = intensity;

        if (area !== 0) {
            saturation = area / (1 - Math.abs(2 * intensity - 1));
            if (max === red) {
                hue = (60 * ((green - blue) / area) + 360) % 360;
            } else if (max === green) {
                hue = (60 * ((blue - red) / area) + 120) % 360;
            } else {
                hue = (60 * ((red - green) / area) + 240) % 360;
            }
        }

        return {
            hex,
            red,
            green,
            blue,
            area,
            hue,
            saturation,
            lightness,
            intensity,
        };
    };
}

// for reference

// type FinalColor = {
//   hex: string;
//   red: number;
//   green: number;
//   blue: number;
//   area: number;
//   hue: number;
//   saturation: number;
//   lightness: number;
//   intensity: number;
// }