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.

shape-styles.ts 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { Theme, ColorStyle, DashStyle, ShapeStyles, SizeStyle } from 'types'
  2. import { lerpColor } from 'utils'
  3. const canvasLight = '#fafafa'
  4. const canvasDark = '#343d45'
  5. const colors = {
  6. [ColorStyle.White]: '#f0f1f3',
  7. [ColorStyle.LightGray]: '#c6cbd1',
  8. [ColorStyle.Gray]: '#788492',
  9. [ColorStyle.Black]: '#212528',
  10. [ColorStyle.Green]: '#36b24d',
  11. [ColorStyle.Cyan]: '#0e98ad',
  12. [ColorStyle.Blue]: '#1c7ed6',
  13. [ColorStyle.Indigo]: '#4263eb',
  14. [ColorStyle.Violet]: '#7746f1',
  15. [ColorStyle.Red]: '#ff2133',
  16. [ColorStyle.Orange]: '#ff9433',
  17. [ColorStyle.Yellow]: '#ffc936',
  18. }
  19. export const strokes: Record<Theme, Record<ColorStyle, string>> = {
  20. light: colors,
  21. dark: {
  22. ...(Object.fromEntries(
  23. Object.entries(colors).map(([k, v]) => [k, lerpColor(v, canvasDark, 0.1)])
  24. ) as Record<ColorStyle, string>),
  25. [ColorStyle.White]: '#ffffff',
  26. [ColorStyle.Black]: '#000',
  27. },
  28. }
  29. export const fills: Record<Theme, Record<ColorStyle, string>> = {
  30. light: {
  31. ...(Object.fromEntries(
  32. Object.entries(colors).map(([k, v]) => [
  33. k,
  34. lerpColor(v, canvasLight, 0.82),
  35. ])
  36. ) as Record<ColorStyle, string>),
  37. [ColorStyle.White]: '#ffffff',
  38. [ColorStyle.Black]: '#ffffff',
  39. },
  40. dark: Object.fromEntries(
  41. Object.entries(colors).map(([k, v]) => [k, lerpColor(v, canvasDark, 0.618)])
  42. ) as Record<ColorStyle, string>,
  43. }
  44. const strokeWidths = {
  45. [SizeStyle.Small]: 2,
  46. [SizeStyle.Medium]: 4,
  47. [SizeStyle.Large]: 8,
  48. }
  49. const fontSizes = {
  50. [SizeStyle.Small]: 24,
  51. [SizeStyle.Medium]: 48,
  52. [SizeStyle.Large]: 72,
  53. auto: 'auto',
  54. }
  55. export function getStrokeWidth(size: SizeStyle): number {
  56. return strokeWidths[size]
  57. }
  58. export function getFontSize(size: SizeStyle): number {
  59. return fontSizes[size]
  60. }
  61. export function getFontStyle(scale: number, style: ShapeStyles): string {
  62. const fontSize = getFontSize(style.size)
  63. return `${fontSize * scale}px/1.4 Verveine Regular`
  64. }
  65. export function getShapeStyle(
  66. style: ShapeStyles,
  67. isDarkMode = false
  68. ): {
  69. stroke: string
  70. fill: string
  71. strokeWidth: number
  72. } {
  73. const { color, size, isFilled } = style
  74. const strokeWidth = getStrokeWidth(size)
  75. const theme: Theme = isDarkMode ? 'dark' : 'light'
  76. return {
  77. stroke: strokes[theme][color],
  78. fill: isFilled ? fills[theme][color] : 'none',
  79. strokeWidth,
  80. }
  81. }
  82. export const defaultStyle: ShapeStyles = {
  83. color: ColorStyle.Black,
  84. size: SizeStyle.Medium,
  85. isFilled: false,
  86. dash: DashStyle.Draw,
  87. }