Skip to content

Commit eddd42f

Browse files
ferdymercurycouet
authored andcommitted
[core] fix non-proportional color resampling
the quotient of extremes 65535/255 was taken as calibration factor, but this is not a proper way of resampling integer variables, that only works well for doubles
1 parent a4a2590 commit eddd42f

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

core/base/src/TColor.cxx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2484,9 +2484,9 @@ ULong_t TColor::RGB2Pixel(Int_t r, Int_t g, Int_t b)
24842484
if (b > 255) b = 255;
24852485

24862486
ColorStruct_t color;
2487-
color.fRed = UShort_t(r * 257); // 65535/255
2488-
color.fGreen = UShort_t(g * 257);
2489-
color.fBlue = UShort_t(b * 257);
2487+
color.fRed = UShort_t(r * 256); // 65536/256
2488+
color.fGreen = UShort_t(g * 256);
2489+
color.fBlue = UShort_t(b * 256);
24902490
color.fMask = kDoRed | kDoGreen | kDoBlue;
24912491
gVirtualX->AllocColor(gVirtualX->GetColormap(), color);
24922492
return color.fPixel;
@@ -2517,9 +2517,13 @@ void TColor::Pixel2RGB(ULong_t pixel, Int_t &r, Int_t &g, Int_t &b)
25172517
ColorStruct_t color;
25182518
color.fPixel = pixel;
25192519
gVirtualX->QueryColor(gVirtualX->GetColormap(), color);
2520-
r = color.fRed / 257;
2521-
g = color.fGreen / 257;
2522-
b = color.fBlue / 257;
2520+
// color is between 0 and 65535 inclusive.
2521+
// We need to move it to the 0 to 255 range (inclusive).
2522+
// So we need to resample the 65536 values into 256 indices.
2523+
// This would mean equal blocks of 256 high-res values per color index.
2524+
r = color.fRed / 256;
2525+
g = color.fGreen / 256;
2526+
b = color.fBlue / 256;
25232527
}
25242528

25252529
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)