1/**2 * Copyright (c) Meta Platforms, Inc. and affiliates.3 *4 * This source code is licensed under the MIT license found in the5 * LICENSE file in the root directory of this source tree.6 */78type RGB = [number, number, number];910const int = Math.floor;1112export class Color {13 constructor(14 private r: number,15 private g: number,16 private b: number,17 ) {}1819 toAlphaString(a: number) {20 return this.toCssString(a);21 }22 toString() {23 return this.toCssString(1);24 }2526 /**27 * Adjust the color by a multiplier to lighten (`> 1.0`) or darken (`< 1.0`) the color. Returns a new28 * instance.29 */30 adjusted(mult: number) {31 const adjusted = Color.redistribute([32 this.r * mult,33 this.g * mult,34 this.b * mult,35 ]);36 return new Color(...adjusted);37 }3839 private toCssString(a: number) {40 return `rgba(${this.r},${this.g},${this.b},${a})`;41 }42 /**43 * Redistributes rgb, maintaing hue until its clamped.44 * https://stackoverflow.com/a/14194345 */46 private static redistribute([r, g, b]: RGB): RGB {47 const threshold = 255.999;48 const max = Math.max(r, g, b);49 if (max <= threshold) {50 return [int(r), int(g), int(b)];51 }52 const total = r + g + b;53 if (total >= 3 * threshold) {54 return [int(threshold), int(threshold), int(threshold)];55 }56 const x = (3 * threshold - total) / (3 * max - total);57 const gray = threshold - x * max;58 return [int(gray + x * r), int(gray + x * g), int(gray + x * b)];59 }60}6162export const BLACK = new Color(0, 0, 0);63export const WHITE = new Color(255, 255, 255);6465const COLOR_POOL = [66 new Color(249, 65, 68),67 new Color(243, 114, 44),68 new Color(248, 150, 30),69 new Color(249, 132, 74),70 new Color(249, 199, 79),71 new Color(144, 190, 109),72 new Color(67, 170, 139),73 new Color(77, 144, 142),74 new Color(87, 117, 144),75 new Color(39, 125, 161),76];7778export function getColorFor(index: number): Color {79 return COLOR_POOL[Math.abs(index) % COLOR_POOL.length]!;80}
Findings
✓ No findings reported for this file.