From 9931e8eeeebf00f45574b8f2e3257ec56d33860f Mon Sep 17 00:00:00 2001 From: nep Date: Fri, 9 Feb 2024 20:46:47 +0100 Subject: [PATCH] rgb_color: Add new APIs: Contrast(), IsLight(), IsDark() - Remove perceptual_brightness - Change implementation of rgb_color::Brightness() to the previous BPrivate::perceptual_brightness() - Introduce convenience methods Contrast(rgb_color), IsLight() and IsDark() Change-Id: Id677d4a32ce43d73bffecf9baf8cffaafb01a16d Reviewed-on: https://review.haiku-os.org/c/haiku/+/7399 Reviewed-by: waddlesplash --- headers/os/interface/GraphicsDefs.h | 22 +++++++++++++++++++++ headers/private/interface/ColorConversion.h | 13 ------------ src/apps/aboutsystem/AboutSystem.cpp | 4 +--- src/kits/interface/GraphicsDefs.cpp | 9 +++++++-- src/kits/tracker/PoseView.cpp | 13 +++++------- src/kits/tracker/PoseView.h | 19 +++--------------- src/kits/tracker/Utilities.cpp | 3 +-- 7 files changed, 39 insertions(+), 44 deletions(-) diff --git a/headers/os/interface/GraphicsDefs.h b/headers/os/interface/GraphicsDefs.h index 7593dedd04..cd0f6571aa 100644 --- a/headers/os/interface/GraphicsDefs.h +++ b/headers/os/interface/GraphicsDefs.h @@ -59,6 +59,28 @@ typedef struct rgb_color { int32 Brightness() const; + inline bool + IsDark() const + { + return Brightness() <= 127; + } + + inline bool + IsLight() const + { + return Brightness() > 127; + } + + static inline int32 + Contrast(rgb_color colorA, rgb_color colorB) + { + int32 contrast = colorA.Brightness() - colorB.Brightness(); + if (contrast < 0) + return -contrast; + + return contrast; + } + inline bool operator==(const rgb_color& other) const { diff --git a/headers/private/interface/ColorConversion.h b/headers/private/interface/ColorConversion.h index 3f5da158ca..e4ddb64f30 100644 --- a/headers/private/interface/ColorConversion.h +++ b/headers/private/interface/ColorConversion.h @@ -65,19 +65,6 @@ private: }; -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 diff --git a/src/apps/aboutsystem/AboutSystem.cpp b/src/apps/aboutsystem/AboutSystem.cpp index 1c9b47d567..6991cb1aa1 100644 --- a/src/apps/aboutsystem/AboutSystem.cpp +++ b/src/apps/aboutsystem/AboutSystem.cpp @@ -913,9 +913,7 @@ SysInfoView::_DesktopTextColor(int32 workspace) const workspace = current_workspace(); rgb_color viewColor = screen.DesktopColor(workspace); - int viewBrightness = BPrivate::perceptual_brightness(viewColor); - textColor.blue = textColor.green = textColor.red = viewBrightness > 127 - ? 0 : 255; + textColor.blue = textColor.green = textColor.red = viewColor.IsLight() ? 0 : 255; textColor.alpha = 255; return textColor; diff --git a/src/kits/interface/GraphicsDefs.cpp b/src/kits/interface/GraphicsDefs.cpp index c37f8e6e93..d14b25bcdd 100644 --- a/src/kits/interface/GraphicsDefs.cpp +++ b/src/kits/interface/GraphicsDefs.cpp @@ -15,6 +15,7 @@ #include #include +#include // patterns const pattern B_SOLID_HIGH = {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}; @@ -36,11 +37,15 @@ const uint32 B_TRANSPARENT_MAGIC_RGBA32_BIG = 0x77747700; const struct screen_id B_MAIN_SCREEN_ID = {0}; -// rgb_color int32 rgb_color::Brightness() const { - return ((int32)red * 41 + (int32)green * 187 + (int32)blue * 28) >> 8; + // 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. + + return (uint8)roundf(sqrtf( + 0.299f * red * red + 0.587f * green * green + 0.114 * blue * blue)); } diff --git a/src/kits/tracker/PoseView.cpp b/src/kits/tracker/PoseView.cpp index 0dfcd7a6ce..bd83e0a1c6 100644 --- a/src/kits/tracker/PoseView.cpp +++ b/src/kits/tracker/PoseView.cpp @@ -9158,16 +9158,13 @@ BPoseView::InvertedBackColor() const { rgb_color background = ui_color(B_DOCUMENT_BACKGROUND_COLOR); rgb_color inverted = invert_color(background); - int textBrightness = BPrivate::perceptual_brightness(inverted); - int viewBrightness = BPrivate::perceptual_brightness(background); - if (abs(viewBrightness - textBrightness) > 127) { - // The colors are different enough, we can use inverted + // The colors are different enough, we can use inverted + if (rgb_color::Contrast(background, inverted) > 127) return inverted; - } else { - // use black or white - return (viewBrightness > 127 ? kBlack : kWhite); - } + + // use black or white + return background.IsLight() ? kBlack : kWhite; } diff --git a/src/kits/tracker/PoseView.h b/src/kits/tracker/PoseView.h index ae7abf3d26..5ce10735c6 100644 --- a/src/kits/tracker/PoseView.h +++ b/src/kits/tracker/PoseView.h @@ -1043,24 +1043,11 @@ BPoseView::DeskTextColor() const rgb_color textColor = HighColor(); rgb_color viewColor = ViewColor(); - 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 + // The colors are different enough, we can use them as is + if (rgb_color::Contrast(viewColor, textColor) > 127) return textColor; - } else { - if (viewBrightness > 127) { - textColor.red = 0; - textColor.green = 0; - textColor.blue = 0; - } else { - textColor.red = 255; - textColor.green = 255; - textColor.blue = 255; - } - return textColor; - } + return viewColor.IsLight() ? kBlack : kWhite; } diff --git a/src/kits/tracker/Utilities.cpp b/src/kits/tracker/Utilities.cpp index cbd7500297..4abf27b71a 100644 --- a/src/kits/tracker/Utilities.cpp +++ b/src/kits/tracker/Utilities.cpp @@ -91,8 +91,7 @@ float ReadOnlyTint(rgb_color base) { // darken tint if read-only (or lighten if dark) - int viewBrightness = BPrivate::perceptual_brightness(base); - return viewBrightness > 127 ? B_DARKEN_1_TINT : 0.85; + return base.IsLight() ? B_DARKEN_1_TINT : 0.85; }