Use proper rounding when casting RGB values to int
When doing an RGB->HSV->RGB roundtrip, the original value should be restored (HSV, being double, has sufficient precision). For e.g. `RGBValue(1,2,3)`, the equivalent resulting code for blue is `trunc(int * 255.0 / 255.0)` (cast from double to int truncates). At least with x87 FP math and its immediate 80bit extended precision the resulting value is ~trunc(2.9999..), i.e. 2, similar problems may exist on other architectures with other values. Using proper rounding avoids the error magnification. Closes https://github.com/wxWidgets/wxWidgets/pull/2078
This commit is contained in:
@@ -3312,9 +3312,9 @@ wxImage::RGBValue wxImage::HSVtoRGB(const HSVValue& hsv)
|
||||
}
|
||||
}
|
||||
|
||||
return RGBValue((unsigned char)(red * 255.0),
|
||||
(unsigned char)(green * 255.0),
|
||||
(unsigned char)(blue * 255.0));
|
||||
return RGBValue((unsigned char)wxRound(red * 255.0),
|
||||
(unsigned char)wxRound(green * 255.0),
|
||||
(unsigned char)wxRound(blue * 255.0));
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user