Tracker: improve algorithm to decide desktop text color

Fixes #16673
This commit is contained in:
Adrien Destugues
2020-12-19 10:17:55 +01:00
parent 4358626708
commit 8a72ba1b54
2 changed files with 37 additions and 17 deletions
@@ -4,6 +4,9 @@
#include <GraphicsDefs.h>
#include <math.h>
class BPoint;
@@ -61,6 +64,20 @@ private:
status_t fCStatus;
};
static inline uint8 perceptual_brightness(rgb_color color)
{
// From http://alienryderflex.com/hsp.html
// Useful in particular to decide if the color is "light" or "dark"
// by checking if the perceptual brightness is > 127.
int r = color.red;
int g = color.green;
int b = color.blue;
return (uint8)roundf(sqrtf(
0.299f * r * r + 0.587f * g * g + 0.114 * b * b));
}
} // namespace BPrivate
#endif
+20 -17
View File
@@ -51,6 +51,7 @@ All rights reserved.
#include <Application.h>
#include <Catalog.h>
#include <Clipboard.h>
#include <ColorConversion.h>
#include <Debug.h>
#include <Dragger.h>
#include <fs_attr.h>
@@ -9035,31 +9036,33 @@ BPoseView::DrawPose(BPose* pose, int32 index, bool fullDraw)
rgb_color
BPoseView::DeskTextColor() const
{
// The desktop color is chosen independently for the desktop.
// The text color is chosen globally for all directories.
// It's fairly easy to get something unreadable (even with the default
// settings, it's expected that text will be black on white in Tracker
// folders, but white on blue on the desktop).
// So here we check if the colors are different enough, and otherwise,
// force the text to be either white or black.
rgb_color textColor = ui_color(B_DOCUMENT_TEXT_COLOR);
rgb_color viewColor = ViewColor();
float readabilityThreshold = abs(textColor.red - viewColor.red)
+ abs(textColor.green - viewColor.green)
+ abs(textColor.blue - viewColor.blue);
if (readabilityThreshold > 384) {
// The readability threshold is highly subjective, but 384 (out of 768)
// seems to be generally suitable for most circumstances.
int textBrightness = BPrivate::perceptual_brightness(textColor);
int viewBrightness = BPrivate::perceptual_brightness(viewColor);
if (abs(viewBrightness - textBrightness) > 127) {
// The colors are different enough, we can use them as is
return textColor;
} else {
float blackWhiteThreshold = viewColor.red
+ (viewColor.green * 1.25f) + (viewColor.blue * 0.45f);
if (blackWhiteThreshold >= 360) {
viewColor.red = 0;
viewColor.green = 0;
viewColor.blue = 0;
if (viewBrightness > 127) {
textColor.red = 0;
textColor.green = 0;
textColor.blue = 0;
} else {
viewColor.red = 255;
viewColor.green = 255;
viewColor.blue = 255;
textColor.red = 255;
textColor.green = 255;
textColor.blue = 255;
}
return viewColor;
return textColor;
}
}