Gamma Table Macros
A Rust procedural macro crate for generating compile-time gamma lookup tables with support for both gamma encoding and gamma correction/decoding.
Overview
This crate provides a proc macro that generates efficient lookup tables for gamma processing at compile time. By default, it uses gamma encoding (inputgamma). 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.
Features
- Compile-time generation: Tables are computed at compile time, resulting in zero runtime overhead
- Dual gamma modes: Gamma encoding (default) and gamma correction/decoding
- Flexible parameters: Configurable gamma values, table sizes, entry types, and brightness limits
- Multiple data types: Support for u8, u16, u32, and u64 entry types
- Brightness limiting: Optional max_value parameter to cap output brightness
Usage
Add this to your Cargo.toml:
[]
= "0.1.0"
Gamma Encoding Example (Default)
use gamma_table;
// Generate a gamma encoding table (default behavior)
gamma_table!
Gamma Correction/Decoding Example
use gamma_table;
// Generate a gamma correction/decoding table
gamma_table!
LED Control Example
For LED control where you want to limit maximum brightness:
gamma_table!
Parameters
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 tosize-1)decoding(optional): Use gamma correction/decoding instead of encoding (defaults tofalse)
Mathematics
Gamma Encoding (Default)
output = ^gamma * max_value
Gamma Correction/Decoding (decoding: true)
output = ^ * max_value
Performance
Since tables are generated at compile time, runtime performance is simply a single array lookup operation - O(1) with no floating-point computation needed.
Examples
Run the examples to see the macro in action:
Testing
Run the test suite:
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.