| Crates.io | gamma-table-macros |
| lib.rs | gamma-table-macros |
| version | 0.1.0 |
| created_at | 2025-06-07 18:55:20.817866+00 |
| updated_at | 2025-06-07 18:55:20.817866+00 |
| description | A procedural macro for generating gamma correction lookup tables |
| homepage | |
| repository | https://github.com/liebman/gamma-table-macros |
| max_upload_size | |
| id | 1704325 |
| size | 66,750 |
A Rust procedural macro crate for generating compile-time gamma lookup tables with support for both gamma encoding and gamma correction/decoding.
This crate provides a proc macro that generates efficient lookup tables for gamma processing at compile time. By default, it uses gamma encoding (input^gamma). It also supports gamma correction/decoding (input^(1/gamma)) when the decoding parameter is set to true. This is particularly useful for graphics applications, LED control, and any scenario where you need fast gamma processing without runtime computation.
Add this to your Cargo.toml:
[dependencies]
gamma-table-macros = "0.1.0"
use gamma_table_macros::gamma_table;
// Generate a gamma encoding table (default behavior)
gamma_table! {
name: GAMMA_ENCODED_TABLE,
entry_type: u8,
gamma: 2.2,
size: 256
}
fn main() {
let input = 128u8; // 50% brightness
let encoded = GAMMA_ENCODED_TABLE[input as usize];
println!("Input: {}, Gamma encoded: {}", input, encoded); // Will be darker
}
use gamma_table_macros::gamma_table;
// Generate a gamma correction/decoding table
gamma_table! {
name: GAMMA_DECODED_TABLE,
entry_type: u8,
gamma: 2.2,
size: 256,
decoding: true
}
fn main() {
let input = 128u8; // 50% brightness
let corrected = GAMMA_DECODED_TABLE[input as usize];
println!("Input: {}, Gamma corrected: {}", input, corrected); // Will be brighter
}
For LED control where you want to limit maximum brightness:
gamma_table! {
name: LED_GAMMA_TABLE,
entry_type: u8,
gamma: 2.5,
size: 256,
max_value: 128 // Limit to 50% max brightness
}
name (required): The name of the const table to be generatedentry_type (required): The unsigned integer type for each entry (u8, u16, u32, u64)gamma (required): The gamma value (positive float)size (required): Number of table entries (minimum 3)max_value (optional): Maximum output value to limit brightness (defaults to size-1)decoding (optional): Use gamma correction/decoding instead of encoding (defaults to false)output = (input / max_input)^gamma * max_value
decoding: true)output = (input / max_input)^(1/gamma) * max_value
Since tables are generated at compile time, runtime performance is simply a single array lookup operation - O(1) with no floating-point computation needed.
Run the examples to see the macro in action:
cargo run --example basic_usage
Run the test suite:
cargo test
Licensed under either of
at your option.