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 <GraphicsDefs.h>
#include <math.h>
class BPoint; class BPoint;
@@ -61,6 +64,20 @@ private:
status_t fCStatus; 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 } // namespace BPrivate
#endif #endif
+20 -17
View File
@@ -51,6 +51,7 @@ All rights reserved.
#include <Application.h> #include <Application.h>
#include <Catalog.h> #include <Catalog.h>
#include <Clipboard.h> #include <Clipboard.h>
#include <ColorConversion.h>
#include <Debug.h> #include <Debug.h>
#include <Dragger.h> #include <Dragger.h>
#include <fs_attr.h> #include <fs_attr.h>
@@ -9035,31 +9036,33 @@ BPoseView::DrawPose(BPose* pose, int32 index, bool fullDraw)
rgb_color rgb_color
BPoseView::DeskTextColor() const 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 textColor = ui_color(B_DOCUMENT_TEXT_COLOR);
rgb_color viewColor = ViewColor(); rgb_color viewColor = ViewColor();
float readabilityThreshold = abs(textColor.red - viewColor.red) int textBrightness = BPrivate::perceptual_brightness(textColor);
+ abs(textColor.green - viewColor.green) int viewBrightness = BPrivate::perceptual_brightness(viewColor);
+ abs(textColor.blue - viewColor.blue); if (abs(viewBrightness - textBrightness) > 127) {
if (readabilityThreshold > 384) { // The colors are different enough, we can use them as is
// The readability threshold is highly subjective, but 384 (out of 768)
// seems to be generally suitable for most circumstances.
return textColor; return textColor;
} else { } else {
float blackWhiteThreshold = viewColor.red if (viewBrightness > 127) {
+ (viewColor.green * 1.25f) + (viewColor.blue * 0.45f); textColor.red = 0;
textColor.green = 0;
if (blackWhiteThreshold >= 360) { textColor.blue = 0;
viewColor.red = 0;
viewColor.green = 0;
viewColor.blue = 0;
} else { } else {
viewColor.red = 255; textColor.red = 255;
viewColor.green = 255; textColor.green = 255;
viewColor.blue = 255; textColor.blue = 255;
} }
return viewColor; return textColor;
} }
} }