fix linear-sRGB color mismatch

This commit is contained in:
numzero 2026-08-05 01:13:38 +03:00
parent 02707dd095
commit 7b4ff19784
2 changed files with 29 additions and 9 deletions

View File

@ -18,7 +18,7 @@ pub struct RedrawArgs {
impl Default for RedrawArgs { impl Default for RedrawArgs {
fn default() -> Self { fn default() -> Self {
Self { Self {
background: vec4(0.05, 0.20, 0.85, 1.00), background: vec4(0.25, 0.49, 0.93, 1.00),
cam_yaw: std::f32::consts::FRAC_PI_4, cam_yaw: std::f32::consts::FRAC_PI_4,
cam_pitch: std::f32::consts::FRAC_PI_6, cam_pitch: std::f32::consts::FRAC_PI_6,
cam_distance: 3., cam_distance: 3.,
@ -71,6 +71,31 @@ fn cube(a: f32, b: f32) -> render::mesh::CpuMeshIndexed {
mesh mesh
} }
fn srgb_to_linear(color: Vec3) -> Vec3 {
color.map(|x| {
if x > 0.04045 {
((x + 0.055) / 1.055).powf(2.4)
} else {
x / 12.92
}
})
}
fn srgba_to_linear(color: Vec4) -> Vec4 {
let alpha = color.w;
let color = srgb_to_linear(vec3(color.x, color.y, color.z));
vec4(color.x, color.y, color.z, alpha)
}
fn color_to_wgpu(color: Vec4) -> wgpu::Color {
wgpu::Color {
r: color.x.into(),
g: color.y.into(),
b: color.z.into(),
a: color.w.into(),
}
}
impl Core { impl Core {
pub fn new(gpu: Gpu, pixel_size: UVec2) -> Self { pub fn new(gpu: Gpu, pixel_size: UVec2) -> Self {
let Gpu { let Gpu {
@ -133,12 +158,7 @@ impl Core {
depth_slice: None, depth_slice: None,
resolve_target: None, resolve_target: None,
ops: wgpu::Operations { ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color { load: wgpu::LoadOp::Clear(color_to_wgpu(srgba_to_linear(args.background))),
r: args.background.x.into(),
g: args.background.y.into(),
b: args.background.z.into(),
a: args.background.w.into(),
}),
store: wgpu::StoreOp::Store, store: wgpu::StoreOp::Store,
}, },
})], })],

View File

@ -168,8 +168,8 @@
<property name="color"> <property name="color">
<color> <color>
<red>0</red> <red>0</red>
<green>32</green> <green>99</green>
<blue>60</blue> <blue>133</blue>
</color> </color>
</property> </property>
</widget> </widget>