All files colorInterpolation.ts

52.08% Statements 25/48
31.82% Branches 7/22
37.5% Functions 3/8
52.08% Lines 25/48

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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 1591x                       1x 1x 1x 1x 1x 1x 1x   1x                   61x               1x                                 1x                                 1x                                 1x                                 1x                                 1x 1x 1x 1x 1x 1x 1x     1x             1x                               61x      
import {
    hslToRGB,
    hsvToRGB,
    labToRGB,
    lchToRGB,
    rgbToHSL,
    rgbToHSV,
    rgbToLAB,
    rgbToLCH,
    rgbToXYZ,
    xyzToRGB,
} from "./colorConverters";
import { ColorHSL } from "./colorHSL";
import { ColorHSV } from "./colorHSV";
import { ColorLAB } from "./colorLAB";
import { ColorLCH } from "./colorLCH";
import { ColorRGBA64 } from "./colorRGBA64";
import { ColorXYZ } from "./colorXYZ";
import { lerp, lerpAnglesInDegrees } from "./mathUtils";
 
export function interpolateRGB(
    position: number,
    left: ColorRGBA64,
    right: ColorRGBA64
): ColorRGBA64 {
    if (isNaN(position) || position <= 0) {
        return left;
    } else if (position >= 1) {
        return right;
    }
    return new ColorRGBA64(
        lerp(position, left.r, right.r),
        lerp(position, left.g, right.g),
        lerp(position, left.b, right.b),
        lerp(position, left.a, right.a)
    );
}
 
export function interpolateHSL(
    position: number,
    left: ColorHSL,
    right: ColorHSL
): ColorHSL {
    if (isNaN(position) || position <= 0) {
        return left;
    } else if (position >= 1) {
        return right;
    }
    return new ColorHSL(
        lerpAnglesInDegrees(position, left.h, right.h),
        lerp(position, left.s, right.s),
        lerp(position, left.l, right.l)
    );
}
 
export function interpolateHSV(
    position: number,
    left: ColorHSV,
    right: ColorHSV
): ColorHSV {
    if (isNaN(position) || position <= 0) {
        return left;
    } else if (position >= 1) {
        return right;
    }
    return new ColorHSV(
        lerpAnglesInDegrees(position, left.h, right.h),
        lerp(position, left.s, right.s),
        lerp(position, left.v, right.v)
    );
}
 
export function interpolateXYZ(
    position: number,
    left: ColorXYZ,
    right: ColorXYZ
): ColorXYZ {
    if (isNaN(position) || position <= 0) {
        return left;
    } else if (position >= 1) {
        return right;
    }
    return new ColorXYZ(
        lerp(position, left.x, right.x),
        lerp(position, left.y, right.y),
        lerp(position, left.z, right.z)
    );
}
 
export function interpolateLAB(
    position: number,
    left: ColorLAB,
    right: ColorLAB
): ColorLAB {
    if (isNaN(position) || position <= 0) {
        return left;
    } else if (position >= 1) {
        return right;
    }
    return new ColorLAB(
        lerp(position, left.l, right.l),
        lerp(position, left.a, right.a),
        lerp(position, left.b, right.b)
    );
}
 
export function interpolateLCH(
    position: number,
    left: ColorLCH,
    right: ColorLCH
): ColorLCH {
    if (isNaN(position) || position <= 0) {
        return left;
    } else if (position >= 1) {
        return right;
    }
    return new ColorLCH(
        lerp(position, left.l, right.l),
        lerp(position, left.c, right.c),
        lerpAnglesInDegrees(position, left.h, right.h)
    );
}
 
export enum ColorInterpolationSpace {
    RGB,
    HSL,
    HSV,
    XYZ,
    LAB,
    LCH,
}
 
export function interpolateByColorSpace(
    position: number,
    space: ColorInterpolationSpace,
    left: ColorRGBA64,
    right: ColorRGBA64
): ColorRGBA64 {
    if (isNaN(position) || position <= 0) {
        return left;
    } else if (position >= 1) {
        return right;
    }
    switch (space) {
        case ColorInterpolationSpace.HSL:
            return hslToRGB(interpolateHSL(position, rgbToHSL(left), rgbToHSL(right)));
        case ColorInterpolationSpace.HSV:
            return hsvToRGB(interpolateHSV(position, rgbToHSV(left), rgbToHSV(right)));
        case ColorInterpolationSpace.XYZ:
            return xyzToRGB(interpolateXYZ(position, rgbToXYZ(left), rgbToXYZ(right)));
        case ColorInterpolationSpace.LAB:
            return labToRGB(interpolateLAB(position, rgbToLAB(left), rgbToLAB(right)));
        case ColorInterpolationSpace.LCH:
            return lchToRGB(interpolateLCH(position, rgbToLCH(left), rgbToLCH(right)));
        default:
            return interpolateRGB(position, left, right);
    }
}