xcode-color-assets icon indicating copy to clipboard operation
xcode-color-assets copied to clipboard

Support for hex RGBA format #RRGGBBAA

Open alinradut opened this issue 3 years ago • 0 comments

Hello!

Would it be possible to add support for hex RGBA #RRGGBBAA along with the current #RRGGBB A%? The reason I am asking is because Figma gives out the color code in this format and it would just streamline the process to be able to copy and paste the values.

I tried to do it myself but my Rust skills are nonexistent and the tests break for the existing format:

fn colorset_value<'a, E: ParseError<&'a str>>(
  input: &'a str,
) -> IResult<&'a str, ColorSetValue, E> {
  preceded(
    space0,
    alt((
      map(hex_alpha_color, ColorSetValue::Color),
      map(hex_color, ColorSetValue::Color),
      map(rgba_color, ColorSetValue::Color),
      map(variable_value, ColorSetValue::Variable),
    )),
  )(input)
}

fn hex_alpha_color<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, Color, E> {
  map(
    tuple((
      char('#'),
      cut(hex_value),
      opt(hex_alpha_value),
    )),
    |res| {
      let (_, rgb, alpha) = res;
      let r = ((rgb >> 16) & 0xff) as u8;
      let g = ((rgb >> 8) & 0xff) as u8;
      let b = (rgb & 0xff) as u8;
      let a = ((alpha.unwrap_or(0xff) & 0xff) as u8) as f32 / 255.0;

      Color { r, g, b, a }
    },
  )(input)
}

fn hex_alpha_value<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, u32, E> {
  context(
    "Hex Alpha Value",
    map_res(take_while_m_n(2, 2, is_hex_digit), parse_hex_value),
  )(input)
}

cargo test output:

---- srgb_asset_catalog stdout ----
thread 'srgb_asset_catalog' panicked at 'Could not parse document: Error { error: "0: at line 5:\n    $black50: #000000 50%\n                      ^\nexpected '\\n', found 5\n\n" }', asset-catalog/tests/asset_catalog.rs:115:40
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
...

It works for me as long as I don't use the percent format, but I would love to see it supported properly. Thanks!

alinradut avatar Feb 07 '22 10:02 alinradut