From 890455affe2d3d3500ca632bbc45a6fa3e815d4e Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Tue, 21 Jan 2025 09:21:36 +0100 Subject: [PATCH] Refactor: Rename function to clarify it's for sRGB only Pull Request: https://projects.blender.org/blender/blender/pulls/133368 --- source/blender/blenfont/intern/blf_glyph.cc | 2 +- source/blender/blenlib/BLI_math_color.h | 18 +++++------------ .../blenlib/intern/math_color_inline.c | 20 ++++++++++++------- .../blender/draw/intern/draw_manager_text.cc | 2 +- .../gizmo_types/button2d_gizmo.cc | 2 +- .../editors/interface/interface_icons.cc | 4 ++-- .../editors/interface/interface_widgets.cc | 10 +++++----- .../regions/interface_region_tooltip.cc | 2 +- .../blender/editors/space_file/file_draw.cc | 4 ++-- .../editors/space_view3d/view3d_utils.cc | 4 ++-- 10 files changed, 33 insertions(+), 35 deletions(-) diff --git a/source/blender/blenfont/intern/blf_glyph.cc b/source/blender/blenfont/intern/blf_glyph.cc index 535c4dd073c..46a6e9dcac4 100644 --- a/source/blender/blenfont/intern/blf_glyph.cc +++ b/source/blender/blenfont/intern/blf_glyph.cc @@ -429,7 +429,7 @@ static GlyphBLF *blf_glyph_cache_add_svg( for (int64_t x = 0; x < int64_t(g->dims[0]); x++) { int64_t offs_in = (y * int64_t(dest_w) * 4) + (x * 4); int64_t offs_out = (y * int64_t(g->dims[0]) + x); - g->bitmap[offs_out] = uchar(float(rgb_to_grayscale_byte(&render_bmp[int64_t(offs_in)])) * + g->bitmap[offs_out] = uchar(float(srgb_to_grayscale_byte(&render_bmp[int64_t(offs_in)])) * (float(render_bmp[int64_t(offs_in + 3)]) / 255.0f)); } } diff --git a/source/blender/blenlib/BLI_math_color.h b/source/blender/blenlib/BLI_math_color.h index c8b0f670578..3255c0fe5b2 100644 --- a/source/blender/blenlib/BLI_math_color.h +++ b/source/blender/blenlib/BLI_math_color.h @@ -147,21 +147,13 @@ void rgb_float_to_uchar(unsigned char r_col[3], const float col_f[3]); void rgba_float_to_uchar(unsigned char r_col[4], const float col_f[4]); /** - * ITU-R BT.709 primaries - * https://en.wikipedia.org/wiki/Relative_luminance + * Compute luminance using Rec.709 primaries, for sRGB and linear Rec.709. * - * Real values are: - * `Y = 0.2126390059(R) + 0.7151686788(G) + 0.0721923154(B)` - * according to: "Derivation of Basic Television Color Equations", RP 177-1993 - * - * As this sums slightly above 1.0, the document recommends to use: - * `0.2126(R) + 0.7152(G) + 0.0722(B)`, as used here. - * - * The high precision values are used to calculate the rounded byte weights so they add up to 255: - * `54(R) + 182(G) + 19(B)` + * Only use for colors known to be in sRGB space, like user interface and themes. + * Scene colors should use #IMB_colormanagement_get_luminance instead. */ -MINLINE float rgb_to_grayscale(const float rgb[3]); -MINLINE unsigned char rgb_to_grayscale_byte(const unsigned char rgb[3]); +MINLINE float srgb_to_grayscale(const float rgb[3]); +MINLINE unsigned char srgb_to_grayscale_byte(const unsigned char rgb[3]); MINLINE int compare_rgb_uchar(const unsigned char col_a[3], const unsigned char col_b[3], diff --git a/source/blender/blenlib/intern/math_color_inline.c b/source/blender/blenlib/intern/math_color_inline.c index 5a1e643c9d4..1f77928c72a 100644 --- a/source/blender/blenlib/intern/math_color_inline.c +++ b/source/blender/blenlib/intern/math_color_inline.c @@ -190,23 +190,29 @@ MINLINE void cpack_cpy_3ub(unsigned char r_col[3], const unsigned int pack) } /* -------------------------------------------------------------------- */ -/** \name RGB/Gray-Scale Functions +/** \name sRGB/Gray-Scale Functions * * \warning - * These are only an approximation, - * in almost _all_ cases, #IMB_colormanagement_get_luminance should be used instead. However for - * screen-only colors which don't depend on the currently loaded profile - this is preferred. - * Checking theme colors for contrast, etc. Basically anything outside the render pipeline. + * Only use for colors known to be in sRGB space, like user interface and themes. + * Scene color should use #IMB_colormanagement_get_luminance instead. * * \{ */ -MINLINE float rgb_to_grayscale(const float rgb[3]) +MINLINE float srgb_to_grayscale(const float rgb[3]) { + /* Real values are: + * `Y = 0.2126390059(R) + 0.7151686788(G) + 0.0721923154(B)` + * according to: "Derivation of Basic Television Color Equations", RP 177-1993 + * + * As this sums slightly above 1.0, the document recommends to use: + * `0.2126(R) + 0.7152(G) + 0.0722(B)`, as used here. */ return (0.2126f * rgb[0]) + (0.7152f * rgb[1]) + (0.0722f * rgb[2]); } -MINLINE unsigned char rgb_to_grayscale_byte(const unsigned char rgb[3]) +MINLINE unsigned char srgb_to_grayscale_byte(const unsigned char rgb[3]) { + /* The high precision values are used to calculate the rounded byte weights so they add up to + * 255: `54(R) + 182(G) + 19(B)` */ return (unsigned char)(((54 * (unsigned short)rgb[0]) + (182 * (unsigned short)rgb[1]) + (19 * (unsigned short)rgb[2])) / 255); diff --git a/source/blender/draw/intern/draw_manager_text.cc b/source/blender/draw/intern/draw_manager_text.cc index 286615dcbfa..2a196c98cb3 100644 --- a/source/blender/draw/intern/draw_manager_text.cc +++ b/source/blender/draw/intern/draw_manager_text.cc @@ -152,7 +152,7 @@ static void drw_text_cache_draw_ex(DRWTextStore *dt, ARegion *region) if (vos->sco[0] != IS_CLIPPED) { if (col_pack_prev != vos->col.pack) { BLF_color4ubv(font_id, vos->col.ub); - const uchar lightness = rgb_to_grayscale_byte(vos->col.ub); + const uchar lightness = srgb_to_grayscale_byte(vos->col.ub); outline_is_dark = lightness > 96; col_pack_prev = vos->col.pack; } diff --git a/source/blender/editors/gizmo_library/gizmo_types/button2d_gizmo.cc b/source/blender/editors/gizmo_library/gizmo_types/button2d_gizmo.cc index 297fbdcfa2e..53166a30529 100644 --- a/source/blender/editors/gizmo_library/gizmo_types/button2d_gizmo.cc +++ b/source/blender/editors/gizmo_library/gizmo_types/button2d_gizmo.cc @@ -227,7 +227,7 @@ static void button2d_draw_intern(const bContext *C, * Use a low value instead of 50% so some darker primary colors * aren't considered being close to black. */ float color_contrast[4]; - copy_v3_fl(color_contrast, rgb_to_grayscale(color) < 0.2f ? 1 : 0); + copy_v3_fl(color_contrast, srgb_to_grayscale(color) < 0.2f ? 1 : 0); color_contrast[3] = color[3]; GPU_shader_uniform_4f(button->shape_batch[i]->shader, "color", UNPACK4(color_contrast)); } diff --git a/source/blender/editors/interface/interface_icons.cc b/source/blender/editors/interface/interface_icons.cc index 43017acc7cc..3dd0bf2b22c 100644 --- a/source/blender/editors/interface/interface_icons.cc +++ b/source/blender/editors/interface/interface_icons.cc @@ -1514,7 +1514,7 @@ static void icon_draw_size(float x, #endif /* If the theme is light, we will adjust the icon colors. */ - const bool invert = (rgb_to_grayscale_byte(btheme->tui.wcol_toolbar_item.inner) > 128); + const bool invert = (srgb_to_grayscale_byte(btheme->tui.wcol_toolbar_item.inner) > 128); const bool geom_inverted = di->data.geom.inverted; /* This could re-generate often if rendered at different sizes in the one interface. @@ -1584,7 +1584,7 @@ static void icon_draw_size(float x, else { UI_GetThemeColor4ubv(TH_TEXT, text_color); } - const bool is_light = rgb_to_grayscale_byte(text_color) > 96; + const bool is_light = srgb_to_grayscale_byte(text_color) > 96; const float zoom_factor = w / UI_ICON_SIZE; uiFontStyle fstyle_small = *UI_FSTYLE_WIDGET; fstyle_small.points *= zoom_factor * 0.8f; diff --git a/source/blender/editors/interface/interface_widgets.cc b/source/blender/editors/interface/interface_widgets.cc index 2f2c03de0e0..af55e73c4be 100644 --- a/source/blender/editors/interface/interface_widgets.cc +++ b/source/blender/editors/interface/interface_widgets.cc @@ -157,8 +157,8 @@ static void color_blend_v4_v4v4(uchar r_col[4], static void color_ensure_contrast_v3(uchar cp[3], const uchar cp_other[3], int contrast) { BLI_assert(contrast > 0); - const int item_value = rgb_to_grayscale_byte(cp); - const int inner_value = rgb_to_grayscale_byte(cp_other); + const int item_value = srgb_to_grayscale_byte(cp); + const int inner_value = srgb_to_grayscale_byte(cp_other); const int delta = item_value - inner_value; if (delta >= 0) { if (contrast > delta) { @@ -2439,7 +2439,7 @@ static void ui_widget_color_disabled(uiWidgetType *wt, const uiWidgetStateInfo * static void widget_active_color(uiWidgetColors *wcol) { - const bool dark = (rgb_to_grayscale_byte(wcol->text) > rgb_to_grayscale_byte(wcol->inner)); + const bool dark = (srgb_to_grayscale_byte(wcol->text) > srgb_to_grayscale_byte(wcol->inner)); color_mul_hsl_v3(wcol->inner, 1.0f, 1.15f, dark ? 1.2f : 1.1f); color_mul_hsl_v3(wcol->outline, 1.0f, 1.15f, 1.15f); color_mul_hsl_v3(wcol->text, 1.0f, 1.15f, dark ? 1.25f : 0.8f); @@ -2610,7 +2610,7 @@ static void widget_state_numslider(uiWidgetType *wt, /* Set the slider 'item' so that it reflects state settings too. * De-saturate so the color of the slider doesn't conflict with the blend color, * which can make the color hard to see when the slider is set to full (see #66102). */ - wt->wcol.item[0] = wt->wcol.item[1] = wt->wcol.item[2] = rgb_to_grayscale_byte(wt->wcol.item); + wt->wcol.item[0] = wt->wcol.item[1] = wt->wcol.item[2] = srgb_to_grayscale_byte(wt->wcol.item); color_blend_v3_v3(wt->wcol.item, color_blend, wcol_state->blend); color_ensure_contrast_v3(wt->wcol.item, wt->wcol.inner, 30); } @@ -3994,7 +3994,7 @@ static void widget_swatch(uiBut *but, const float width = rect->xmax - rect->xmin; const float height = rect->ymax - rect->ymin; /* find color luminance and change it slightly */ - float bw = rgb_to_grayscale(col); + float bw = srgb_to_grayscale(col); bw += (bw < 0.5f) ? 0.5f : -0.5f; diff --git a/source/blender/editors/interface/regions/interface_region_tooltip.cc b/source/blender/editors/interface/regions/interface_region_tooltip.cc index 89f5738c69b..628ae11064e 100644 --- a/source/blender/editors/interface/regions/interface_region_tooltip.cc +++ b/source/blender/editors/interface/regions/interface_region_tooltip.cc @@ -314,7 +314,7 @@ static void ui_tooltip_region_draw_cb(const bContext * /*C*/, ARegion *region) float border_color[4] = {1.0f, 1.0f, 1.0f, 0.15f}; float bgcolor[4]; UI_GetThemeColor4fv(TH_BACK, bgcolor); - if (rgb_to_grayscale(bgcolor) > 0.5f) { + if (srgb_to_grayscale(bgcolor) > 0.5f) { border_color[0] = 0.0f; border_color[1] = 0.0f; border_color[2] = 0.0f; diff --git a/source/blender/editors/space_file/file_draw.cc b/source/blender/editors/space_file/file_draw.cc index 5cbd6d7e59e..45f62ba456d 100644 --- a/source/blender/editors/space_file/file_draw.cc +++ b/source/blender/editors/space_file/file_draw.cc @@ -651,7 +651,7 @@ static void file_draw_preview(const FileDirEntry *file, float border_color[4] = {1.0f, 1.0f, 1.0f, 0.15f}; float bgcolor[4]; UI_GetThemeColor4fv(TH_BACK, bgcolor); - if (rgb_to_grayscale(bgcolor) > 0.5f) { + if (srgb_to_grayscale(bgcolor) > 0.5f) { border_color[0] = 0.0f; border_color[1] = 0.0f; border_color[2] = 0.0f; @@ -719,7 +719,7 @@ static void file_draw_special_image(const FileDirEntry *file, /* Small icon in the middle of large image, scaled to fit container and UI scale */ float icon_opacity = 0.4f; uchar icon_color[4] = {0, 0, 0, 255}; - if (rgb_to_grayscale(document_img_col) < 0.5f) { + if (srgb_to_grayscale(document_img_col) < 0.5f) { icon_color[0] = 255; icon_color[1] = 255; icon_color[2] = 255; diff --git a/source/blender/editors/space_view3d/view3d_utils.cc b/source/blender/editors/space_view3d/view3d_utils.cc index 6de3710085e..85430c14e12 100644 --- a/source/blender/editors/space_view3d/view3d_utils.cc +++ b/source/blender/editors/space_view3d/view3d_utils.cc @@ -90,7 +90,7 @@ void ED_view3d_text_colors_get(const Scene *scene, /* Default text color from TH_TEXT_HI. If it is too close * to the background color, darken or lighten it. */ UI_GetThemeColor3fv(TH_TEXT_HI, r_text_color); - float text_lightness = rgb_to_grayscale(r_text_color); + float text_lightness = srgb_to_grayscale(r_text_color); float bg_color[3]; ED_view3d_background_color_get(scene, v3d, bg_color); const float distance = len_v3v3(r_text_color, bg_color); @@ -105,7 +105,7 @@ void ED_view3d_text_colors_get(const Scene *scene, } /* Shadow color is black or white depending on final text lightness. */ - text_lightness = rgb_to_grayscale(r_text_color); + text_lightness = srgb_to_grayscale(r_text_color); if (text_lightness > 0.4f) { copy_v3_fl(r_shadow_color, 0.0f); }