{"id":20410906,"url":"https://github.com/mintlu8/colorthis","last_synced_at":"2025-10-17T18:45:00.463Z","repository":{"id":200149061,"uuid":"705248276","full_name":"mintlu8/colorthis","owner":"mintlu8","description":"Meta macros that aid macro authors to create colors from a generalized syntax.","archived":false,"fork":false,"pushed_at":"2023-10-15T14:49:40.000Z","size":17,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-28T04:49:14.867Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mintlu8.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-10-15T13:37:36.000Z","updated_at":"2023-10-15T14:26:39.000Z","dependencies_parsed_at":null,"dependency_job_id":"f7f60fd7-f4ab-408b-96e9-a290536e09d3","html_url":"https://github.com/mintlu8/colorthis","commit_stats":null,"previous_names":["mintlu8/colorthis"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mintlu8%2Fcolorthis","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mintlu8%2Fcolorthis/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mintlu8%2Fcolorthis/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mintlu8%2Fcolorthis/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mintlu8","download_url":"https://codeload.github.com/mintlu8/colorthis/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241955047,"owners_count":20048405,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-15T05:49:01.025Z","updated_at":"2025-10-17T18:44:55.433Z","avatar_url":"https://github.com/mintlu8.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# colorthis\n\n[![Crates.io](https://img.shields.io/crates/v/colorthis.svg)](https://crates.io/crates/colorthis)\n[![Docs](https://docs.rs/colorthis/badge.svg)](https://docs.rs/colorthis/latest/colorthis/)\n\nMeta macros that aid macro authors to create colors from a generalized syntax.\n\n## General Syntax\n\n```rust\nrgb!($path: path, $color_syntax: tt [=\u003e {$($fields: ident)*}])\n```\n\nWhere path is a function or a struct constructor\n\nIf fields are not specified,\n\n```rust\nrgba!(Cmyk, [123, 155, 224, 155]);\n```\n\nproduces\n\n```rust\nCmyk(123, 155, 224, 155);\n```\n\nIf fields are specified,\n\n```rust\nrgba!(Cmyk, [123, 155, 224, 155] =\u003e {c, m, y, k});\n```\n\nproduces\n\n```rust\nCmyk {\n    c: 123,\n    m: 155,\n    y: 224,\n    k: 155,\n};\n```\n\nPopulating a larger struct is allowed as long as the struct implements Default.\n\n```rust\n# use colorthis::rgba;\n#[derive(Default)]\nstruct PixelData {\n    r: u8, g: u8, b: u8, a: u8,\n    z: u8, brightness: u8, magic_number: u8\n}\nrgba!(PixelData, [1, 2, 3, 4] =\u003e {r, g, b, a, _});\n```\n\nproduces\n\n```rust\nPixelData {\n    r: 1,\n    g: 2,\n    b: 3,\n    a: 4,\n    ..Default::default()\n};\n```\n\n## Example usage\n\n```rust\nmacro_rules! bevy_rgba {\n    ($color: tt) =\u003e {\n        ::colorthis::rgbaf!(::bevy::prelude::Color::Rgba, $color =\u003e {red, green, blue, alpha})\n    };\n    ($($colors: literal),* $(,)?) =\u003e {\n        ::colorthis::rgbaf!(::bevy::prelude::Color::Rgba, [$($colors),*] =\u003e {red, green, blue, alpha})\n    };\n}\nbevy_rgba!(\"00EEFFFF\");\nbevy_rgba!(\"#AAEE22\");\nbevy_rgba!(0xAABBCC);\nbevy_rgba!(72, 184, 255, 255);\nbevy_rgba!(0.23, 0.44, 0.89, 0.50);\nbevy_rgba!(Red);\nbevy_rgba!(Violet800);\n```\n\n## Color Syntax\n\nThe color is always a TokenTree `tt`.\n\n* Bracketed numbers: `[0.3, 0.72, 0.98]`, `[124, 54, 87, 255]`\n* Parenthesised expressions: `(0.3, 0.72, 0.98)`, `(r, g, b + g, a + 0.5)`\n* Splat syntax: `[0.3; 3]`, `[0.3; 3, 0.8]`, `[0.7; 4]`\n* Hex strings: `\"AABBCC\"`, `\"AABBCCFF\"`, `\"#AABBCC\"`, `\"#AABBCCFF\"`\n* Hex number literals: `0xAABBCC`, `0xAABBCCFF`\n* CSS color names: `Red`, `Blue`\n* TailwindCSS color names: `Red100`, `Sky400`\n\n## Details\n\n### Bracketed Numbers\n\nIf all values are integers,\nthey are considered to be in range `0..=255`.\n\n```rust\nassert_eq!(rgba!(Color, [32, 144, 220, 125]), Color(32,144,220,125));\n```\n\nIf any value is a float,\nvalues are considered to be in range `0.0..=1.0`.\n\nTherefore this fails to compile since numbers like 144 overflows this bounds.\n\n```rust\nassert_eq!(rgba!(Color, [0.5, 144, 220, 125]), Color(32, 144, 220, 125));\n```\n\nHowever if all values are `0`s or `1`s, and the macro invoked\nis `rgbf!()` or `rgbaf!()` `1`s are treated as `1.0`s.\n\n```rust\nassert_eq!(rgbaf!(ColorF, [255, 255, 255, 255]), ColorF(1.0, 1.0, 1.0, 1.0));\nassert_eq!(rgbaf!(ColorF, [1.0, 0.5, 1, 0]), ColorF(1.0, 0.5, 1.0, 0.0));\nassert_eq!(rgbaf!(ColorF, [1, 0, 1, 0]), ColorF(1.0, 0.0, 1.0, 0.0));\n```\n\n### Parenthesised Expressions\n\nCurrently we do not modify the expression or provide type conversion,\naside from validating the number of expressions,\nand providing default alpha value if needed.\n\n### Splat Syntax\n\n* `[v; 3]` means `[v, v, v, 255]`\n* `[v; 3, a]` means `[v, v, v, a]`\n* `[v; 4]` means `[v, v, v, v]`\n\n### Color Names\n\nWe relies on a [crate](https://docs.rs/parse-color/0.1.0/parse_color/)\nto parse and generate these data at compile time. No external support required.\n\n## Feature Flags\n\n### `unchecked` and `clamp`\n\nBy default, we assert integers are in `0..=255`, floats are in `0.0..=1.0`\n\nif unchecked is not specified, this fails\n\n```rust\nrgba!(color, [1000, 255, 128, 0]); // 1000 is not in 0..=255\n```\n\nWith `unchecked`, this *might* compile, assuming color accepts `1000` as an input.\n\n```rust\nrgba!(color, [1000, 255, 128, 0]);\n```\n\nWith `clamp`, this compiles and clamps `1000` into `255`\n\n```rust\nrgba!(color, [1000, 255, 128, 0]); // produces color(255, 255, 128, 0)\n```\n\n## `compact`\n\nCompact allows 3 or 4 letter compact colors to be compiled.\n\n```rust\nrgba!(color, 0xABC); // compiles to 0xAABBCC\nrgba!(color, \"1234\"); // compiles to \"11223344\"\nrgba!(color, \"#FFF\"); // compiles to \"FFFFFF\"\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmintlu8%2Fcolorthis","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmintlu8%2Fcolorthis","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmintlu8%2Fcolorthis/lists"}