logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://anongit.hacktivis.me/git/pleroma-fe.git/

theme3_slot_functions.js (3403B)


  1. import { convert, brightness } from 'chromatism'
  2. import { alphaBlend, getTextColor, relativeLuminance } from '../color_convert/color_convert.js'
  3. export const process = (text, functions, { findColor, findShadow }, { dynamicVars, staticVars }) => {
  4. const { funcName, argsString } = /\$(?<funcName>\w+)\((?<argsString>[#a-zA-Z0-9-,.'"\s]*)\)/.exec(text).groups
  5. const args = argsString.split(/,/g).map(a => a.trim())
  6. const func = functions[funcName]
  7. if (args.length < func.argsNeeded) {
  8. throw new Error(`$${funcName} requires at least ${func.argsNeeded} arguments, but ${args.length} were provided`)
  9. }
  10. return func.exec(args, { findColor, findShadow }, { dynamicVars, staticVars })
  11. }
  12. export const colorFunctions = {
  13. alpha: {
  14. argsNeeded: 2,
  15. exec: (args, { findColor }, { dynamicVars, staticVars }) => {
  16. const [color, amountArg] = args
  17. const colorArg = convert(findColor(color, { dynamicVars, staticVars })).rgb
  18. const amount = Number(amountArg)
  19. return { ...colorArg, a: amount }
  20. }
  21. },
  22. textColor: {
  23. argsNeeded: 2,
  24. exec: (args, { findColor }, { dynamicVars, staticVars }) => {
  25. const [backgroundArg, foregroundArg, preserve = 'preserve'] = args
  26. const background = convert(findColor(backgroundArg, { dynamicVars, staticVars })).rgb
  27. const foreground = convert(findColor(foregroundArg, { dynamicVars, staticVars })).rgb
  28. return getTextColor(background, foreground, preserve === 'preserve')
  29. }
  30. },
  31. blend: {
  32. argsNeeded: 3,
  33. exec: (args, { findColor }, { dynamicVars, staticVars }) => {
  34. const [backgroundArg, amountArg, foregroundArg] = args
  35. const background = convert(findColor(backgroundArg, { dynamicVars, staticVars })).rgb
  36. const foreground = convert(findColor(foregroundArg, { dynamicVars, staticVars })).rgb
  37. const amount = Number(amountArg)
  38. return alphaBlend(background, amount, foreground)
  39. }
  40. },
  41. mod: {
  42. argsNeeded: 2,
  43. exec: (args, { findColor }, { dynamicVars, staticVars }) => {
  44. const [colorArg, amountArg] = args
  45. const color = convert(findColor(colorArg, { dynamicVars, staticVars })).rgb
  46. const amount = Number(amountArg)
  47. const effectiveBackground = dynamicVars.lowerLevelBackground
  48. const isLightOnDark = relativeLuminance(convert(effectiveBackground).rgb) < 0.5
  49. const mod = isLightOnDark ? 1 : -1
  50. return brightness(amount * mod, color).rgb
  51. }
  52. }
  53. }
  54. export const shadowFunctions = {
  55. borderSide: {
  56. argsNeeded: 3,
  57. exec: (args, { findColor }) => {
  58. const [color, side, alpha = '1', widthArg = '1', inset = 'inset'] = args
  59. const width = Number(widthArg)
  60. const isInset = inset === 'inset'
  61. const targetShadow = {
  62. x: 0,
  63. y: 0,
  64. blur: 0,
  65. spread: 0,
  66. color,
  67. alpha: Number(alpha),
  68. inset: isInset
  69. }
  70. side.split('-').forEach((position) => {
  71. switch (position) {
  72. case 'left':
  73. targetShadow.x = width * (inset ? 1 : -1)
  74. break
  75. case 'right':
  76. targetShadow.x = -1 * width * (inset ? 1 : -1)
  77. break
  78. case 'top':
  79. targetShadow.y = width * (inset ? 1 : -1)
  80. break
  81. case 'bottom':
  82. targetShadow.y = -1 * width * (inset ? 1 : -1)
  83. break
  84. }
  85. })
  86. return [targetShadow]
  87. }
  88. }
  89. }