You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
954 B

3 years ago
  1. // @ts-ignore
  2. import kelvinToRgb from "kelvin-to-rgb";
  3. import {ColorValue} from "../models/Colors";
  4. import iro from "@jaames/iro";
  5. export function kelvinToColorValue(rawKelvin: number): ColorValue {
  6. const kelvin = normalizeKelvin(rawKelvin);
  7. return kelvinTable[kelvin];
  8. }
  9. export function kelvinToRgbHex(rawKelvin: number): string {
  10. const kelvin = normalizeKelvin(rawKelvin);
  11. const c = new iro.Color();
  12. c.kelvin = kelvin;
  13. return c.hexString;
  14. }
  15. function normalizeKelvin(kelvin: number): number {
  16. if (kelvin < 1000) {
  17. return 1000;
  18. } else if (kelvin > 10000) {
  19. return 10000;
  20. } else {
  21. return kelvin - (kelvin % 100);
  22. }
  23. }
  24. const kelvinTable: {[key: number]: ColorValue} = {};
  25. for (let kelvin = 1000; kelvin <= 10000; kelvin += 100) {
  26. const c = new iro.Color();
  27. c.kelvin = kelvin;
  28. const [h, s] = [c.hsv.h, (c.hsv.s as number) / 100];
  29. kelvinTable[kelvin] = {h, s, kelvin};
  30. }