All files / app/codeCharta/util/color colorConverter.ts

100% Statements 68/68
100% Branches 5/5
100% Functions 19/19
100% Lines 67/67

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 142103x 103x 103x   103x 103x 103x 103x     298x 298x 26x 26x     298x       130x 130x 12x 12x     130x       4x 4x 2x 2x     4x       566x       2x 2x 2x       122x 122x       150x 150x       261x 783x 783x         26x 26x       27x 27x       12x 12x       1x 1x 1x   1x       47x 47x 47x 47x   47x 235x 235x 235x   47x       4x 4x 4x         276x 276x     2x   276x               4x       8x 8x 8x 8x 8x 8x       4x      
import { Color, Vector3 } from "three"
import convert from "color-convert"
import { HSL } from "./hsl"
 
export class ColorConverter {
    private static colorToVector3Map = new Map<string, Vector3>()
    private static colorToVector3ArrayMap = new Map<string, number[]>()
    private static hexToNumberMap = new Map<string, number>()
 
    static getVector3(color: string) {
        let vector = this.colorToVector3Map.get(color)
        if (vector === undefined) {
            vector = ColorConverter.colorToVector3(color)
            this.colorToVector3Map.set(color, vector)
        }
 
        return vector
    }
 
    static getVector3Array(color: string) {
        let array = this.colorToVector3ArrayMap.get(color)
        if (array === undefined) {
            array = ColorConverter.colorToVector3Array(color)
            this.colorToVector3ArrayMap.set(color, array)
        }
 
        return array
    }
 
    static getNumber(hex: string) {
        let number = this.hexToNumberMap.get(hex)
        if (number === undefined) {
            number = ColorConverter.convertHexToNumber(hex)
            this.hexToNumberMap.set(hex, number)
        }
 
        return number
    }
 
    static convertHexToNumber(hex: string) {
        return Number(`0x${hex.slice(1)}`)
    }
 
    static convertNumberToHex(colorNumber: number) {
        const hexColor = colorNumber.toString(16)
        const zeros = "0".repeat(6 - hexColor.length)
        return `#${zeros}${hexColor}`
    }
 
    static convertHexToRgba(hex: string, opacity = 1) {
        const rgbColor = this.encodeHex(hex)
        return `rgba(${rgbColor.join(",")},${opacity})`
    }
 
    static convertHexToColorObject(hex: string) {
        const rgbColor = this.encodeHex(hex)
        return new Color(...rgbColor)
    }
 
    static convertColorToHex(colorObject: Color) {
        return [colorObject.r, colorObject.g, colorObject.b].reduce((string, color) => {
            string += Math.round(color).toString(16).padStart(2, "0")
            return string
        }, "#")
    }
 
    static hexToHSL(hex: string) {
        const hsl = convert.hex.hsl(hex)
        return new HSL(...hsl)
    }
 
    static colorToVector3(color: string) {
        const convertedColor = ColorConverter.convertHexToNumber(color)
        return new Vector3(((convertedColor >> 16) & 0xff) / 255, ((convertedColor >> 8) & 0xff) / 255, (convertedColor & 0xff) / 255)
    }
 
    static colorToVector3Array(color: string) {
        const convertedColor = ColorConverter.convertHexToNumber(color)
        return [((convertedColor >> 16) & 0xff) / 255, ((convertedColor >> 8) & 0xff) / 255, (convertedColor & 0xff) / 255]
    }
 
    static vector3ToRGB(vector: Vector3) {
        const r = Math.floor(vector.x * 255)
        const g = Math.floor(vector.y * 255)
        const b = Math.floor(vector.z * 255)
 
        return new Color(r, g, b)
    }
 
    static gradient(startColor: string, endColor: string, steps: number) {
        const start = this.convertHexToColorObject(startColor)
        const end = this.convertHexToColorObject(endColor)
        const diff = end.sub(start)
        const stepsArray: string[] = []
 
        for (let index = 0; index <= steps; index++) {
            const stepDiff = diff.clone().multiplyScalar((1 / steps) * index)
            const step = start.clone().add(stepDiff)
            stepsArray[index] = this.convertColorToHex(step)
        }
        return stepsArray
    }
 
    static getImageDataUri(hex: string) {
        const rgbColor = this.encodeHex(hex)
        const encodedRGBColor = this.encodeRGB(rgbColor[0], rgbColor[1], rgbColor[2])
        return this.generatePixel(encodedRGBColor)
    }
 
    private static encodeHex(string: string) {
        // Cut off the hash sign.
        let hex = string.slice(1)
        if (hex.length === 3) {
            // Always use the six character hex color encoding instead of the shorter
            // three character one.
            hex = `${hex[0]}${hex[0]}${hex[1]}${hex[1]}${hex[2]}${hex[2]}`
        }
        return [
            Number.parseInt(`${hex[0]}${hex[1]}`, 16),
            Number.parseInt(`${hex[2]}${hex[3]}`, 16),
            Number.parseInt(`${hex[4]}${hex[5]}`, 16)
        ]
    }
 
    private static encodeRGB(r: number, g: number, b: number) {
        return this.encodeTriplet(0, r, g) + this.encodeTriplet(b, 255, 255)
    }
 
    private static encodeTriplet(a: number, b: number, c: number) {
        const keyString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
        const enc1 = a >> 2
        const enc2 = ((a & 3) << 4) | (b >> 4)
        const enc3 = ((b & 15) << 2) | (c >> 6)
        const enc4 = c & 63
        return keyString.charAt(enc1) + keyString.charAt(enc2) + keyString.charAt(enc3) + keyString.charAt(enc4)
    }
 
    private static generatePixel(color: string) {
        return `data:image/gif;base64,R0lGODlhAQABAPAA${color}/yH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==`
    }
}