Refactor: Rename function to clarify it's for sRGB only

Pull Request: https://projects.blender.org/blender/blender/pulls/133368
This commit is contained in:
Brecht Van Lommel
2025-01-21 09:21:36 +01:00
parent 5e02b4e6f1
commit 890455affe
10 changed files with 33 additions and 35 deletions

View File

@@ -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));
}
}

View File

@@ -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],

View File

@@ -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);

View File

@@ -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;
}

View File

@@ -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));
}

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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);
}