All files / fast-colors/src rgb.ts

100% Statements 20/20
100% Branches 12/12
100% Functions 4/4
100% Lines 20/20

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      1x 122x 109x 109x 109x 109x 109x 109x     57x   52x                                 122x   61x   55x   6x           26x                 31x                 4x           26x                 26x                 3x      
/**
 * @deprecated
 */
export function hexToRgb(hex: string): string {
    const processedHex: RgbJSON = properRGBValues(hex);
    const r: string = processedHex.r;
    const g: string = processedHex.g;
    const b: string = processedHex.b;
    const a: string = processedHex.a;
    const rgb: boolean = processedHex.rgb;
    const rgba: boolean = processedHex.rgba;
 
    if (rgb) {
        return `rgb(${parseInt(r, 16)}, ${parseInt(g, 16)}, ${parseInt(b, 16)})`;
    } else if (rgba) {
        return `rgba(${parseInt(r, 16)}, ${parseInt(g, 16)}, ${parseInt(
            b,
            16
        )}, ${parseInt(a, 16) / 255})`;
    }
}
 
interface RgbJSON {
    r: string;
    g: string;
    b: string;
    a: string;
    rgb: boolean;
    rgba: boolean;
}
 
function properRGBValues(hex: string): RgbJSON {
    hex = hex.replace("#", "");
    if (3 === hex.length || 6 === hex.length) {
        return processRGBHex(hex);
    } else if (4 === hex.length || 8 === hex.length) {
        return processRGBAHex(hex);
    } else {
        throw new Error(`#${hex} is not a convertible hex value`);
    }
}
 
function processRGBHex(hex: string): RgbJSON {
    if (3 === hex.length && /^([A-Fa-f0-9]{3}){1,2}$/.test(hex)) {
        return {
            r: hex.charAt(0) + hex.charAt(0),
            g: hex.charAt(1) + hex.charAt(1),
            b: hex.charAt(2) + hex.charAt(2),
            a: "",
            rgb: true,
            rgba: false,
        };
    } else if (6 === hex.length && /^([A-Fa-f0-9]{3}){1,2}$/.test(hex)) {
        return {
            r: hex.substring(0, 2),
            g: hex.substring(2, 4),
            b: hex.substring(4, 6),
            a: "",
            rgb: true,
            rgba: false,
        };
    } else {
        throw new Error(`#${hex} is not a convertible hex value`);
    }
}
 
function processRGBAHex(hex: string): RgbJSON {
    if (4 === hex.length && /^([A-Fa-f0-9]{4}){1,2}$/.test(hex)) {
        return {
            r: hex.charAt(0) + hex.charAt(0),
            g: hex.charAt(1) + hex.charAt(1),
            b: hex.charAt(2) + hex.charAt(2),
            a: hex.charAt(3) + hex.charAt(3),
            rgb: false,
            rgba: true,
        };
    } else if (8 === hex.length && /^([A-Fa-f0-9]{4}){1,2}$/.test(hex)) {
        return {
            r: hex.substring(0, 2),
            g: hex.substring(2, 4),
            b: hex.substring(4, 6),
            a: hex.substring(6, 8),
            rgb: false,
            rgba: true,
        };
    } else {
        throw new Error(`#${hex} is not a convertible hex value`);
    }
}