Skip to content

Commit

Permalink
Fixes incorrect color brightness check (#9677)
Browse files Browse the repository at this point in the history
* color change

* Fix comment

* more method

* split procs
  • Loading branch information
EvilDragonfiend authored Nov 27, 2023
1 parent db2a3d9 commit b320f97
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 18 deletions.
32 changes: 30 additions & 2 deletions code/__HELPERS/colors.dm
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,39 @@
#define RANDOM_COLOUR (rgb(rand(0,255),rand(0,255),rand(0,255)))

/// Given a color in the format of "#RRGGBB", will return if the color
/// is dark.
/proc/is_color_dark(color, threshold = 25)
/// is dark. Value is mixed with Saturation and Brightness from HSV.
/proc/is_color_dark_with_saturation(color, threshold = 25)
var/hsl = rgb2num(color, COLORSPACE_HSL)
return hsl[3] < threshold

/// it checks if a color is dark, but without saturation value.
/// This uses Brightness only, without Saturation from HSV
/proc/is_color_dark_without_saturation(color, threshold = 25)
return get_color_brightness_from_hex(color) < threshold

/// returns HSV brightness 0 to 100 by color hex
/proc/get_color_brightness_from_hex(A)
if(!A || length(A) != length_char(A))
return 0
var/R = hex2num(copytext(A, 2, 4))
var/G = hex2num(copytext(A, 4, 6))
var/B = hex2num(copytext(A, 6, 8))
return round(max(R, G, B)/2.55, 1)

// currently unused proc, but made for someone who will need it.
/// returns HSV saturation 0 to 100 by color hex
/proc/get_color_saturation_from_hex(A)
if(!A || length(A) != length_char(A))
return 0
var/R = hex2num(copytext(A, 2, 4))
var/G = hex2num(copytext(A, 4, 6))
var/B = hex2num(copytext(A, 6, 8))
var/brightness = max(R, G, B)
if(brightness == 0)
return 0

return round((brightness - min(R, G, B))/brightness*100, 1)

/// Given a 3 character color (no hash), converts it into #RRGGBB (with hash)
/proc/expand_three_digit_color(color)
if (length_char(color) != 3)
Expand Down
9 changes: 0 additions & 9 deletions code/__HELPERS/type2type.dm
Original file line number Diff line number Diff line change
Expand Up @@ -473,15 +473,6 @@ Takes a string and a datum. The string is well, obviously the string being check
if(var_source.vars.Find(A))
. += A

/// Converts a hex code to a number
/proc/color_hex2num(A)
if(!A || length(A) != length_char(A))
return 0
var/R = hex2num(copytext(A, 2, 4))
var/G = hex2num(copytext(A, 4, 6))
var/B = hex2num(copytext(A, 6, 8))
return R+G+B

//word of warning: using a matrix like this as a color value will simplify it back to a string after being set
/proc/color_hex2color_matrix(string)
var/length = length(string)
Expand Down
4 changes: 2 additions & 2 deletions code/game/objects/items/crayons.dm
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@

if(isobj(target) && !(target.flags_1 & UNPAINTABLE_1))
if(actually_paints)
if(color_hex2num(paint_color) < 350 && !istype(target, /obj/structure/window)) //Colors too dark are rejected
if(is_color_dark_without_saturation(paint_color, 33) && !istype(target, /obj/structure/window)) //Colors too dark are rejected
if(isclothing(target))
var/obj/item/clothing/C = target
if(((C.flags_cover & HEADCOVERSEYES) || (C.flags_cover & MASKCOVERSEYES) || (C.flags_cover & GLASSESCOVERSEYES)) && !HAS_TRAIT(C, TRAIT_SPRAYPAINTED))
Expand All @@ -716,7 +716,7 @@

target.add_atom_colour(paint_color, WASHABLE_COLOUR_PRIORITY)
if(istype(target, /obj/structure/window))
if(color_hex2num(paint_color) < 255)
if(is_color_dark_without_saturation(paint_color, 50))
target.set_opacity(255)
else
target.set_opacity(initial(target.opacity))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
if (!..(value))
return FALSE

if (is_color_dark(expand_three_digit_color(value)))
if (is_color_dark_with_saturation(expand_three_digit_color(value)))
return FALSE

return TRUE
6 changes: 3 additions & 3 deletions code/modules/holoparasite/holoparasite_builder.dm
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
)
),
"validation" = list(
"color" = is_color_dark(accent_color, HOLOPARA_MAX_ACCENT_LIGHTNESS) ? "too dark" : "valid",
"color" = is_color_dark_with_saturation(accent_color, HOLOPARA_MAX_ACCENT_LIGHTNESS) ? "too dark" : "valid",
"name" = check_name_validity(),
"notes" = check_notes_validity()
)
Expand Down Expand Up @@ -196,7 +196,7 @@
if(!istext(color) || length(color) != 7)
return
var/new_accent_color = sanitize_hexcolor(color, desired_format = 6, include_crunch = TRUE, default = (length(accent_color) == 7 && accent_color != initial(accent_color)) ? accent_color : pick(GLOB.color_list_blood_brothers))
if(is_color_dark(new_accent_color, HOLOPARA_MAX_ACCENT_LIGHTNESS))
if(is_color_dark_with_saturation(new_accent_color, HOLOPARA_MAX_ACCENT_LIGHTNESS))
to_chat(usr, "<span class='warning'>Selected accent color is too dark!</span>")
return
accent_color = new_accent_color
Expand Down Expand Up @@ -346,7 +346,7 @@
to_chat(src, "<span class='warning'>The provided notes contain forbidden words.</span>")
user.balloon_alert(user, "failed, filtered notes", show_in_chat = FALSE)
return FALSE
if(is_color_dark(accent_color, HOLOPARA_MAX_ACCENT_LIGHTNESS))
if(is_color_dark_with_saturation(accent_color, HOLOPARA_MAX_ACCENT_LIGHTNESS))
to_chat(src, "<span class='warning'>The provided accent color ([accent_color]) is too dark (lightness of [rgb2num(accent_color, COLORSPACE_HSL)[3]], must be below [HOLOPARA_MAX_ACCENT_LIGHTNESS]).</span>")
user.balloon_alert(user, "failed, accent color too dark", show_in_chat = FALSE)
return FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@
new_color = tgui_color_picker(user, "Choose a new color for [src]'s flashlight.", "Light Color",light_color)
if(!new_color)
return
if(is_color_dark(new_color, 50) ) //Colors too dark are rejected
if(is_color_dark_with_saturation(new_color, 50) ) //Colors too dark are rejected
to_chat(user, "<span class='warning'>That color is too dark! Choose a lighter one.</span>")
new_color = null
return set_flashlight_color(new_color)
Expand Down

0 comments on commit b320f97

Please sign in to comment.