utils.ts 879 B

1234567891011121314151617181920212223242526272829
  1. export function hexToRGBA(hex: string, opacity: number): string {
  2. hex = hex.replace('#', '')
  3. const r = parseInt(hex.slice(0, 2), 16)
  4. const g = parseInt(hex.slice(2, 4), 16)
  5. const b = parseInt(hex.slice(4, 6), 16)
  6. // Returning an RGB color object
  7. return `rgba(${r},${g},${b},${opacity.toString()})`
  8. }
  9. /**
  10. * Since strings cannot be directly assigned to the 'style' attribute in JSX,
  11. * this method transforms the string into an object representation of the styles.
  12. */
  13. export function CssTransform(cssString: string): object {
  14. if (cssString.length === 0)
  15. return {}
  16. const style: object = {}
  17. const propertyValuePairs = cssString.split(';')
  18. for (const pair of propertyValuePairs) {
  19. if (pair.trim().length > 0) {
  20. const [property, value] = pair.split(':')
  21. Object.assign(style, { [property.trim()]: value.trim() })
  22. }
  23. }
  24. return style
  25. }