All files / src/mathf color.ts

86.11% Statements 31/36
81.81% Branches 9/11
66.66% Functions 8/12
86.11% Lines 31/36

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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243                                                            2x 2x 2x 2x   2x                                   2x                                                                                                     1x                                       6x                                                 3x 3x 3x 3x                       12x 12x       12x 12x 12x 12x             12x                                 33x 10x     23x     10x 10x     23x 22x     22x 22x             22x           1x                             6x    
/**
 * A library that helps with color transformations.
 *
 * @see https://www.alanzucconi.com/2016/01/06/colour-interpolation/
 */
 
import * as is from '../is/is';
import * as mathf from '../mathf/mathf';
 
export interface ColorRGBA {
  r: number;
  g: number;
  b: number;
  a: number;
}
 
export interface ColorRGB {
  r: number;
  g: number;
  b: number;
}
/**
 * Converts a hex value such as 0xffffff to normalized Rbg values.
 *
 * ```ts
 * color.hexToRgbNormalized(0xffffff) // >> [1, 1, 1];
 * ```
 * @param hexValue
 */
export function hexToRgbNormalized(hexValue: number) {
  const out = [];
  out[0] = ((hexValue >> 16) & 0xff) / 255;
  out[1] = ((hexValue >> 8) & 0xff) / 255;
  out[2] = (hexValue & 0xff) / 255;
 
  return out;
}
 
/**
 * Converts a colorRGB to an rgb array.
 */
export function colorRgbToRgb(colorRgb: ColorRGB): Array<number> {
  return [colorRgb.r, colorRgb.g, colorRgb.b];
}
 
/**
 * Converts normalized rgb values back out to hex.
 * ```ts
 * color.normalizedRgbToHex([1,1,1]) // >> 0xFFFFFF
 * ```
 * @param rgb
 */
export function normalizedRgbToHex(rgb: Array<number>) {
  return ((rgb[0] * 255) << 16) + ((rgb[1] * 255) << 8) + ((rgb[2] * 255) | 0);
}
 
/**
 * Converts rgb to normalized rgb.
 * ```ts
 * color.rgbToNormalizedRgb([255,255,255]) // >> [1,1,1]
 * color.rgbToNormalizedRgb([0,0,0]) // >> [0,0,0]
 * ```
 *
 * @param rgb
 */
export function rgbToNormalizedRgb(rgb: Array<number>): Array<number> {
  return [rgb[0] / 255, rgb[1] / 255, rgb[2] / 255];
}
 
/**
 * Converts normalized rgb to rgb.
 * ```ts
 * color.normalizedRgbToRgb([1,1,1]) // >> [255,255,255]
 * color.normalizedRgbToRgb([0,0,0]) // >> [0,0,0]
 * ```
 *
 * @param rgb
 */
export function normalizedRgbToRgb(rgb: Array<number>): Array<number> {
  return [(rgb[0] * 255) >> 0, (rgb[1] * 255) >> 0, (rgb[2] * 255) >> 0];
}
 
/**
 * A stardard lerp between two rgba values.  This simple algo is a
 * straight line interpolation and isn't always going to
 * guarantee the right hue between values.  In short,
 * it's not gonna be very colorful between values.
 *
 * ```ts
 *
 * let a = { r: 0, g: 0, b: 0, a: 0};
 * let b = { r: 255, g: 255, b: 255, a: 1};
 * let lerp = color.rgbaLerp(a, b, 0.5);
 * ```
 *
 * @param start The starting rgba color.
 * @param end The ending rgba color.
 * @param t The current progress.j
 */
export function rgbaLerp(
  start: ColorRGBA,
  end: ColorRGBA,
  t: number
): ColorRGBA {
  return {
    r: mathf.lerp(start.r, end.r, t) >> 0,
    g: mathf.lerp(start.g, end.g, t) >> 0,
    b: mathf.lerp(start.b, end.b, t) >> 0,
    a: mathf.lerp(start.a, end.a, t),
  };
}
 
/**
 * Similar to to [[color.rgbaLerp]] but uses ease.
 * @param start
 * @param end
 * @param t
 */
export function rgbaEase(
  start: ColorRGBA,
  end: ColorRGBA,
  t: number,
  ease: Function
): ColorRGBA {
  return {
    r: mathf.ease(start.r, end.r, t, ease) >> 0,
    g: mathf.ease(start.g, end.g, t, ease) >> 0,
    b: mathf.ease(start.b, end.b, t, ease) >> 0,
    a: mathf.ease(start.a, end.a, t, ease),
  };
}
 
/**
 * Converts an rgb out to a standard hex value.
 *
 * Modified version of:
 * https://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb
 *
 * ```ts
 *
 * color.rgbToHex({r: 255, g: 255, b: 255}); // #FFFFFF
 * color.rgbToHex({r: 0, g: 0, b: 0}); // #000000
 *
 * // You can pass RGBA but it will simply get ignored.
 * color.rgbToHex({r: 255, g: 255, b: 255, a: 1}); // #FFFFFF
 * ```
 * @param rgb
 */
export function rgbToHex(rgb: ColorRGB | ColorRGBA): string {
  const r = rgb.r;
  const g = rgb.g;
  const b = rgb.b;
  return (
    '#' +
    ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1).toUpperCase()
  );
}
 
/**
 * Converts a hex value to rgb.
 * Modified version of:
 * https://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb
 */
export function hexToRgba(hex: string, a = 1): ColorRGBA | null {
  const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
  hex = hex.replace(shorthandRegex, (m, r, g, b) => {
    return r + r + g + g + b + b;
  });
 
  const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  let rgba = null;
  Eif (result) {
    rgba = {
      r: parseInt(result[1], 16),
      g: parseInt(result[2], 16),
      b: parseInt(result[3], 16),
      a: a,
    };
  }
  return rgba;
}
 
/**
 * Converts a css like rgba string to an RGBA object.
 *
 * ```ts
 *
 * color.cssToRgba('#FFFFFF') // { r: 255, b: 255, g: 255, a: 1}
 * color.cssToRgba('rgba(255, 255, 255, 0.3)') // { r: 255, b: 255, g: 255, a: 0.3}
 * color.cssToRgb('rgb(255, 255, 255)') // { r: 255, b: 255, g: 255, a: 1}
 *
 * color.cssToRgba(20) // null
 * color.cssToRgba('hello') // null
 * ```
 */
export function cssToRgba(css: string): ColorRGBA | null {
  if (is.cssHex(css)) {
    return hexToRgba(css);
  }
 
  if (is.cssRgb(css)) {
    // We are just going to modify the rgb string to be rgba like
    // and process it as a rgba type string.
    css = css.replace(')', ',1)');
    css = css.replace('rgb', 'rgba');
  }
 
  if (is.cssRgba(css)) {
    const match = css.match(
      /^rgba\(\s*?(\d{1,3})\s*?,\s*?(\d{1,3})\s*?,\s*?(\d{1,3})\s*?,\s*?(\d*(?:\.\d+)?)\)$/
    );
    if (match) {
      const rgba = {
        r: +match[1],
        g: +match[2],
        b: +match[3],
        a: +match[4],
      };
 
      return rgba;
    } else E{
      return null;
    }
  }
 
  return null;
}
 
/**
 * Converts ColorRGBA to a css string.
 *
 * ```ts
 *
 * color.rgbaToCss({r: 255, g: 255, b: 255, a: 1}); // rgba(255, 255, 255, 1)
 *
 * ```
 *
 * @param rgba
 */
export function rgbaToCss(rgba: ColorRGBA): string {
  return `rgba(${rgba.r}, ${rgba.g}, ${rgba.b}, ${rgba.a})`;
}