Skip to content

Commit

Permalink
Compute proper intensity of the pixels for a TTF text colour.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberth289346 committed Dec 16, 2024
1 parent 101a4de commit 3ac6fdc
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions CorsixTH/Src/th_gfx_sdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1204,12 +1204,25 @@ bool sprite_sheet::get_sprite_average_colour(size_t iSprite,
uint8_t cPalIndex = pSprite->data[i];
uint32_t iColour = palette->get_argb_data()[cPalIndex];
if ((iColour >> 24) == 0) continue;


// - Convert from nonlinear RGB to linear RGB.
// gamma = around 2.2 but depends on the screen.
// R, G and B are fractions.
const double gamma = 2.2;
double linR = std::pow(palette::get_red(iColour) / 255.0d, gamma);

Check failure on line 1213 in CorsixTH/Src/th_gfx_sdl.cpp

View workflow job for this annotation

GitHub Actions / Windows

invalid literal suffix 'd'; literal operator or literal operator template 'operator ""d' not found [D:\a\CorsixTH\CorsixTH\build\win-x64-rel\CorsixTH\CorsixTH_lib.vcxproj]
double linG = std::pow(palette::get_green(iColour) / 255.0d, gamma);

Check failure on line 1214 in CorsixTH/Src/th_gfx_sdl.cpp

View workflow job for this annotation

GitHub Actions / Windows

invalid literal suffix 'd'; literal operator or literal operator template 'operator ""d' not found [D:\a\CorsixTH\CorsixTH\build\win-x64-rel\CorsixTH\CorsixTH_lib.vcxproj]
double linB = std::pow(palette::get_blue(iColour) / 255.0d, gamma);

Check failure on line 1215 in CorsixTH/Src/th_gfx_sdl.cpp

View workflow job for this annotation

GitHub Actions / Windows

invalid literal suffix 'd'; literal operator or literal operator template 'operator ""d' not found [D:\a\CorsixTH\CorsixTH\build\win-x64-rel\CorsixTH\CorsixTH_lib.vcxproj]
// - Compute luminance Y in XYZ space.
// Y = .2126 * R^gamma + .7152 * G^gamma + .0722 * B^gamma
double Y = 0.2126 * linR + 0.7152 * linG + 0.0722 * linB;
// - Compute lightness L*.
// L* = 116 * Y ^ 1/3 - 16, range is from 0 to 100.
double L = std::min(100.0d, std::max(0.0d, 166 * std::cbrt(L) - 16));

Check failure on line 1221 in CorsixTH/Src/th_gfx_sdl.cpp

View workflow job for this annotation

GitHub Actions / Windows

invalid literal suffix 'd'; literal operator or literal operator template 'operator ""d' not found [D:\a\CorsixTH\CorsixTH\build\win-x64-rel\CorsixTH\CorsixTH_lib.vcxproj]

Check failure on line 1221 in CorsixTH/Src/th_gfx_sdl.cpp

View workflow job for this annotation

GitHub Actions / Windows

invalid literal suffix 'd'; literal operator or literal operator template 'operator ""d' not found [D:\a\CorsixTH\CorsixTH\build\win-x64-rel\CorsixTH\CorsixTH_lib.vcxproj]

Check failure on line 1221 in CorsixTH/Src/th_gfx_sdl.cpp

View workflow job for this annotation

GitHub Actions / Windows

'std::max': no matching overloaded function found [D:\a\CorsixTH\CorsixTH\build\win-x64-rel\CorsixTH\CorsixTH_lib.vcxproj]

Check failure on line 1221 in CorsixTH/Src/th_gfx_sdl.cpp

View workflow job for this annotation

GitHub Actions / Windows

'std::min': no matching overloaded function found [D:\a\CorsixTH\CorsixTH\build\win-x64-rel\CorsixTH\CorsixTH_lib.vcxproj]
uint8_t cIntensity = static_cast<uint8_t>(L / 100 * 255.0d);

Check failure on line 1222 in CorsixTH/Src/th_gfx_sdl.cpp

View workflow job for this annotation

GitHub Actions / Windows

invalid literal suffix 'd'; literal operator or literal operator template 'operator ""d' not found [D:\a\CorsixTH\CorsixTH\build\win-x64-rel\CorsixTH\CorsixTH_lib.vcxproj]

// Grant higher score to pixels with high or low intensity (helps avoid
// grey fonts)
int iR = palette::get_red(iColour);
int iG = palette::get_green(iColour);
int iB = palette::get_blue(iColour);
uint8_t cIntensity = static_cast<uint8_t>((iR + iG + iB) / 3);
int iScore = 1 + std::max(0, 3 - ((255 - cIntensity) / 32)) +
std::max(0, 3 - (cIntensity / 32));
iUsageCounts[cPalIndex] += iScore;
Expand Down

0 comments on commit 3ac6fdc

Please sign in to comment.