If you’re absolutely sure your bytes are coming out as 0xRR,0xGG,0xBB,0xAA, then the only remaining culprit is almost always sRGB ↔ linear conversion on the way into your swapchain. By default people use a …Srgb swapchain format (e.g. Bgra8UnormSrgb), which means:
• You hand wgpu “linear” numbers in [0.0–1.0],
• wgpu converts them back into sRGB when writing to the frame buffer.
If you simply feed it your hex-as-f64 (e.g. r = 0.5 for 0x80), it will treat that 0.5 as linear and then gamma‐encode it for sRGB storage—making it look too bright (and “washed out”) on screen.
Two ways to fix
-
Pre-linearize your sRGB hex values
fn srgb_to_linear(c: f64) -> f64 {
// exact IEC‐61966-2.1 piecewise formula
if c <= 0.04045 {
c / 12.92
} else {
((c + 0.055) / 1.055).powf(2.4)
}
}
impl From<U32color> for wgpu::Color {
fn from(value: U32color) -> Self {
let [r8, g8, b8, a8] = value.0.to_be_bytes();
let sr = r8 as f64 / 255.0;
let sg = g8 as f64 / 255.0;
let sb = b8 as f64 / 255.0;
let sa = a8 as f64 / 255.0;
wgpu::Color {
r: srgb_to_linear(sr),
g: srgb_to_linear(sg),
b: srgb_to_linear(sb),
a: sa, // alpha is linear already
}
}
}
-
Switch to a non-sRGB format on your surface
let surface_format = wgpu::TextureFormat::Bgra8Unorm; // ← no "Srgb"
// … or Rgba8Unorm, etc.
surface_config.format = surface_format;
Now the GPU will not do any gamma‐correction for you, so feeding raw [0.0–1.0] sRGB values will show up exactly as you expect.
Quick sanity check
Right after you decompose to [r8,g8,b8,a8], print them (and/or the floats) to be 100% sure you’re seeing what you think:
eprintln!("bytes = {:02X},{:02X},{:02X},{:02X}", r8, g8, b8, a8);
eprintln!("floats = {:.3},{:.3},{:.3},{:.3}", sr, sg, sb, sa);
If those match your hex literal, then it’s definitely the sRGB/linear mismatch. Once you either pre-linearize or use a UNorm format, your on-screen color will match the hex. 🎨✨