material-foundation / material-color-utilities

Color libraries for Material You
Apache License 2.0
1.69k stars 150 forks source link

Add RGBA utility #38

Open SebHex opened 2 years ago

SebHex commented 2 years ago

We have hex conversion utilities (hexFromArgb and argbFromHex) already, RGBA conversion utilites would also be a nice quality of life addition!

I'd offer a pull request, but https://github.com/material-foundation/material-color-utilities/issues/11#issuecomment-977794871

TypeScript implementation:

import * as colorUtils from './color_utils';

interface Rgba {
  r: number
  g: number
  b: number
  a: number
}

/**
 * @param argb ARGB representation of a color.
 * @return RGBA representation of a color.
 */
const rbgaFromArgb = (argb: number): Rgba => {
  const r = colorUtils.redFromArgb(argb)
  const g = colorUtils.greenFromArgb(argb)
  const b = colorUtils.blueFromArgb(argb)
  const a = colorUtils.alphaFromArgb(argb)

  return { r, g, b, a }
}

/**
 * @param rgba RGBA representation of a color.
 * @returns ARGB representation of a color.
 */
const argbFromRgba = (rgba: Rgba): number => {
  return (rgba.a << 24) | (rgba.r << 16) | (rgba.g << 8) | rgba.b
}
rodydavis commented 1 year ago

Adding this internally and should be on a future update!