Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for hex RGBA format #RRGGBBAA #13

Open
alinradut opened this issue Feb 7, 2022 · 0 comments
Open

Support for hex RGBA format #RRGGBBAA #13

alinradut opened this issue Feb 7, 2022 · 0 comments

Comments

@alinradut
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant