color.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. /** RGB color triplet expressed as a single number */
  7. export type Color = { readonly '@type': 'color' } & number
  8. export function Color(hex: number) { return hex as Color }
  9. export namespace Color {
  10. export function toStyle(hexColor: Color) {
  11. return `rgb(${hexColor >> 16 & 255}, ${hexColor >> 8 & 255}, ${hexColor & 255})`
  12. }
  13. export function toHexString(hexColor: Color) {
  14. return '0x' + ('000000' + hexColor.toString(16)).slice(-6)
  15. }
  16. export function toRgb(hexColor: Color) {
  17. return [ hexColor >> 16 & 255, hexColor >> 8 & 255, hexColor & 255 ]
  18. }
  19. export function toRgbNormalized(hexColor: Color) {
  20. return [ (hexColor >> 16 & 255) / 255, (hexColor >> 8 & 255) / 255, (hexColor & 255) / 255 ]
  21. }
  22. export function fromRgb(r: number, g: number, b: number): Color {
  23. return ((r << 16) | (g << 8) | b) as Color
  24. }
  25. export function fromNormalizedRgb(r: number, g: number, b: number): Color {
  26. return (((r * 255) << 16) | ((g * 255) << 8) | (b * 255)) as Color
  27. }
  28. export function fromArray(array: Helpers.NumberArray, offset: number): Color {
  29. return fromRgb(array[offset], array[offset + 1], array[offset + 2])
  30. }
  31. export function fromNormalizedArray(array: Helpers.NumberArray, offset: number): Color {
  32. return fromNormalizedRgb(array[offset], array[offset + 1], array[offset + 2])
  33. }
  34. /** Copies hex color to rgb array */
  35. export function toArray(hexColor: Color, array: Helpers.NumberArray, offset: number) {
  36. array[ offset ] = (hexColor >> 16 & 255)
  37. array[ offset + 1 ] = (hexColor >> 8 & 255)
  38. array[ offset + 2 ] = (hexColor & 255)
  39. return array
  40. }
  41. /** Copies normalized (0 to 1) hex color to rgb array */
  42. export function toArrayNormalized<T extends Helpers.NumberArray>(hexColor: Color, array: T, offset: number) {
  43. array[ offset ] = (hexColor >> 16 & 255) / 255
  44. array[ offset + 1 ] = (hexColor >> 8 & 255) / 255
  45. array[ offset + 2 ] = (hexColor & 255) / 255
  46. return array
  47. }
  48. /** Linear interpolation between two colors */
  49. export function interpolate(c1: Color, c2: Color, t: number): Color {
  50. const r1 = c1 >> 16 & 255
  51. const g1 = c1 >> 8 & 255
  52. const b1 = c1 & 255
  53. const r2 = c2 >> 16 & 255
  54. const g2 = c2 >> 8 & 255
  55. const b2 = c2 & 255
  56. const r = r1 + (r2 - r1) * t
  57. const g = g1 + (g2 - g1) * t
  58. const b = b1 + (b2 - b1) * t
  59. return ((r << 16) | (g << 8) | b) as Color
  60. }
  61. }
  62. export type ColorTable<T extends { [k: string]: number[] }> = { [k in keyof T]: Color[] }
  63. export function ColorTable<T extends { [k: string]: number[] }>(o: T) { return o as ColorTable<T> }
  64. export type ColorMap<T extends { [k: string]: number }> = { [k in keyof T]: Color }
  65. export function ColorMap<T extends { [k: string]: number }>(o: T) { return o as ColorMap<T> }