Add hex <-> sRGB color conversions and tests#14
Conversation
Introduces Tokenscript implementations for converting between hexadecimal and sRGB color formats. Updates the sRGB color schema to include these conversions and adds comprehensive unit tests to verify correctness and round-trip consistency.
There was a problem hiding this comment.
Pull request overview
This PR adds bidirectional color conversion between hexadecimal and sRGB formats, enabling seamless transformations between the web-standard hex notation (e.g., #ff0000) and the normalized sRGB color space (0-1 range). The implementation mirrors the existing RGB-to-hex conversion patterns and includes comprehensive test coverage.
Changes:
- Added
from-hex.tokenscriptto convert hex colors to sRGB (supports both 3-digit and 6-digit formats) - Added
to-hex.tokenscriptto convert sRGB colors to hex format - Updated srgb-color schema.json to register both conversion directions
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/schemas/types/srgb-color/from-hex.tokenscript | Implements hex to sRGB conversion by parsing hex strings and normalizing RGB values to 0-1 range |
| src/schemas/types/srgb-color/to-hex.tokenscript | Implements sRGB to hex conversion by scaling 0-1 values to 0-255 and formatting as hex strings |
| src/schemas/types/srgb-color/schema.json | Registers bidirectional hex conversion definitions with lossless flags |
| src/schemas/types/srgb-color/unit.test.ts | Adds comprehensive test suites for conversions including edge cases, boundary values, and round-trip consistency |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| variable rgba: List = {input}.r * 255, {input}.g * 255, {input}.b * 255; | ||
| variable hex: String = "#"; | ||
| variable i: Number = 0; | ||
| variable value: Number = 0; | ||
|
|
||
| while(i < 3) [ | ||
| value = round(rgba.get(i)); |
There was a problem hiding this comment.
The variable is named rgba but only contains RGB values (no alpha channel). Consider renaming to rgb for clarity, as sRGB colors in this conversion don't include an alpha channel. Compare with rgb-color/to-hex.tokenscript where the rgba name is justified because that type can include an optional alpha property that is handled later in the script.
| variable rgba: List = {input}.r * 255, {input}.g * 255, {input}.b * 255; | |
| variable hex: String = "#"; | |
| variable i: Number = 0; | |
| variable value: Number = 0; | |
| while(i < 3) [ | |
| value = round(rgba.get(i)); | |
| variable rgb: List = {input}.r * 255, {input}.g * 255, {input}.b * 255; | |
| variable hex: String = "#"; | |
| variable i: Number = 0; | |
| variable value: Number = 0; | |
| while(i < 3) [ | |
| value = round(rgb.get(i)); |
Introduces Tokenscript implementations for converting between hexadecimal and sRGB color formats. Updates the sRGB color schema to include these conversions and adds comprehensive unit tests to verify correctness and round-trip consistency.