From f3983e39010016b6529f7d9d8abbbce90e4c794d Mon Sep 17 00:00:00 2001 From: Ghom <42542238+Ghommie@users.noreply.github.com> Date: Sun, 24 Dec 2023 21:14:01 +0100 Subject: [PATCH] Color matrix defines for filters and identity (#80320) This PR converts the procs `color_matrix_identity`, `color_matrix_lightness` and `color_matrix_contrast` into defines/macros. Also adds in defines for common color matrix filters. I don't like how `color_matrix_identity` is a one-line proc with no arg. Also these help people identify the sort of color matrix filter is being used. --- code/__DEFINES/matrices.dm | 24 +++++++++++++++ code/__HELPERS/matrices.dm | 29 +++++-------------- code/__HELPERS/type2type.dm | 4 +-- .../proximity_monitor/fields/timestop.dm | 2 +- code/game/turfs/open/lava.dm | 2 +- .../view_variables/color_matrix_editor.dm | 2 +- .../admin/view_variables/get_variables.dm | 2 +- code/modules/client/client_colour.dm | 2 +- code/modules/mod/mod_paint.dm | 4 +-- 9 files changed, 40 insertions(+), 31 deletions(-) diff --git a/code/__DEFINES/matrices.dm b/code/__DEFINES/matrices.dm index 1bb86f9ff47..26ff5a7232a 100644 --- a/code/__DEFINES/matrices.dm +++ b/code/__DEFINES/matrices.dm @@ -1,3 +1,27 @@ /// Helper macro for creating a matrix at the given offsets. /// Works at compile time. #define TRANSLATE_MATRIX(offset_x, offset_y) matrix(1, 0, (offset_x), 0, 1, (offset_y)) +/// The color matrix of an image which colors haven't been altered. Does nothing. +#define COLOR_MATRIX_IDENTITY list(1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1, 0,0,0,0) +/// Color inversion +#define COLOR_MATRIX_INVERT list(-1,0,0,0, 0,-1,0,0, 0,0,-1,0, 0,0,0,1, 1,1,1,0) +///Sepiatone +#define COLOR_MATRIX_SEPIATONE list(0.393,0.349,0.272,0, 0.769,0.686,0.534,0, 0.189,0.168,0.131,0, 0,0,0,1, 0,0,0,0) +///Grayscale +#define COLOR_MATRIX_GRAYSCALE list(0.33,0.33,0.33,0, 0.59,0.59,0.59,0, 0.11,0.11,0.11,0, 0,0,0,1, 0,0,0,0) +///Polaroid colors +#define COLOR_MATRIX_POLAROID list(1.438,-0.062,-0.062,0, -0.122,1.378,-0.122,0, -0.016,-0.016,1.483,0, 0,0,0,1, 0,0,0,0) +/// Converts reds to blue, green to red and blue to green. +#define COLOR_MATRIX_BRG list(0,0,1,0, 0,1,0,0, 1,0,0,0, 0,0,0,1, 0,0,0,0) +/// Black & White +#define COLOR_MATRIX_BLACK_WHITE list(1.5,1.5,1.5,0, 1.5,1.5,1.5,0, 1.5,1.5,1.5,0, 0,0,0,1, -1,-1,-1,0) +/** + * Adds/subtracts overall lightness + * 0 is identity, 1 makes everything white, -1 makes everything black + */ +#define COLOR_MATRIX_LIGHTNESS(power) list(1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1, power,power,power,0) +/** + * Changes distance colors have from rgb(127,127,127) grey + * 1 is identity. 0 makes everything grey >1 blows out colors and greys + */ +#define COLOR_MATRIX_CONTRAST(val) list(val,0,0,0, 0,val,0,0, 0,0,val,0, 0,0,0,1, (1-val)*0.5,(1-val)*0.5,(1-val)*0.5,0) diff --git a/code/__HELPERS/matrices.dm b/code/__HELPERS/matrices.dm index 075a5e5ae0e..e6a9e038849 100644 --- a/code/__HELPERS/matrices.dm +++ b/code/__HELPERS/matrices.dm @@ -102,15 +102,6 @@ list(-1,0,0,0, 0,-1,0,0, 0,0,-1,0, 0,0,0,1, 1,1,1,0) list(0.393,0.349,0.272,0, 0.769,0.686,0.534,0, 0.189,0.168,0.131,0, 0,0,0,1, 0,0,0,0) */ -//Does nothing -/proc/color_matrix_identity() - return list(1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1, 0,0,0,0) - -//Adds/subtracts overall lightness -//0 is identity, 1 makes everything white, -1 makes everything black -/proc/color_matrix_lightness(power) - return list(1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1, power,power,power,0) - //Changes distance hues have from grey while maintaining the overall lightness. Greys are unaffected. //1 is identity, 0 is greyscale, >1 oversaturates colors /proc/color_matrix_saturation(value) @@ -121,12 +112,6 @@ list(0.393,0.349,0.272,0, 0.769,0.686,0.534,0, 0.189,0.168,0.131,0, 0,0,0,1, 0,0 return list(R + value,R,R,0, G,G + value,G,0, B,B,B + value,0, 0,0,0,1, 0,0,0,0) -//Changes distance colors have from rgb(127,127,127) grey -//1 is identity. 0 makes everything grey >1 blows out colors and greys -/proc/color_matrix_contrast(value) - var/add = (1 - value) / 2 - return list(value,0,0,0, 0,value,0,0, 0,0,value,0, 0,0,0,1, add,add,add,0) - //Moves all colors angle degrees around the color wheel while maintaining intensity of the color and not affecting greys //0 is identity, 120 moves reds to greens, 240 moves reds to blues /proc/color_matrix_rotate_hue(angle) @@ -159,9 +144,9 @@ round(cos_inv_third+sqrt3_sin, 0.001), round(cos_inv_third-sqrt3_sin, 0.001), ro //Returns a matrix addition of A with B /proc/color_matrix_add(list/A, list/B) if(!istype(A) || !istype(B)) - return color_matrix_identity() + return COLOR_MATRIX_IDENTITY if(A.len != 20 || B.len != 20) - return color_matrix_identity() + return COLOR_MATRIX_IDENTITY var/list/output = list() output.len = 20 for(var/value in 1 to 20) @@ -171,9 +156,9 @@ round(cos_inv_third+sqrt3_sin, 0.001), round(cos_inv_third-sqrt3_sin, 0.001), ro //Returns a matrix multiplication of A with B /proc/color_matrix_multiply(list/A, list/B) if(!istype(A) || !istype(B)) - return color_matrix_identity() + return COLOR_MATRIX_IDENTITY if(A.len != 20 || B.len != 20) - return color_matrix_identity() + return COLOR_MATRIX_IDENTITY var/list/output = list() output.len = 20 var/x = 1 @@ -191,14 +176,14 @@ round(cos_inv_third+sqrt3_sin, 0.001), round(cos_inv_third-sqrt3_sin, 0.001), ro */ /proc/color_to_full_rgba_matrix(color, return_identity_on_fail = TRUE) if(!color) - return color_matrix_identity() + return COLOR_MATRIX_IDENTITY if(istext(color)) var/list/L = ReadRGB(color) if(!L) var/message = "Invalid/unsupported color ([color]) argument in color_to_full_rgba_matrix()" if(return_identity_on_fail) stack_trace(message) - return color_matrix_identity() + return COLOR_MATRIX_IDENTITY CRASH(message) return list(L[1]/255,0,0,0, 0,L[2]/255,0,0, 0,0,L[3]/255,0, 0,0,0,L.len>3?L[4]/255:1, 0,0,0,0) if(!islist(color)) //invalid format @@ -231,7 +216,7 @@ round(cos_inv_third+sqrt3_sin, 0.001), round(cos_inv_third-sqrt3_sin, 0.001), ro var/message = "Invalid/unsupported color (list of length [L.len]) argument in color_to_full_rgba_matrix()" if(return_identity_on_fail) stack_trace(message) - return color_matrix_identity() + return COLOR_MATRIX_IDENTITY CRASH(message) #undef LUMA_R diff --git a/code/__HELPERS/type2type.dm b/code/__HELPERS/type2type.dm index 3b2d52fbbb6..98e48852094 100644 --- a/code/__HELPERS/type2type.dm +++ b/code/__HELPERS/type2type.dm @@ -337,7 +337,7 @@ GLOBAL_LIST_INIT(modulo_angle_to_dir, list(NORTH,NORTHEAST,EAST,SOUTHEAST,SOUTH, /proc/color_hex2color_matrix(string) var/length = length(string) if((length != 7 && length != 9) || length != length_char(string)) - return color_matrix_identity() + return COLOR_MATRIX_IDENTITY var/r = hex2num(copytext(string, 2, 4))/255 var/g = hex2num(copytext(string, 4, 6))/255 var/b = hex2num(copytext(string, 6, 8))/255 @@ -345,7 +345,7 @@ GLOBAL_LIST_INIT(modulo_angle_to_dir, list(NORTH,NORTHEAST,EAST,SOUTHEAST,SOUTH, if(length == 9) a = hex2num(copytext(string, 8, 10))/255 if(!isnum(r) || !isnum(g) || !isnum(b) || !isnum(a)) - return color_matrix_identity() + return COLOR_MATRIX_IDENTITY return list(r,0,0,0, 0,g,0,0, 0,0,b,0, 0,0,0,a, 0,0,0,0) //will drop all values not on the diagonal diff --git a/code/datums/proximity_monitor/fields/timestop.dm b/code/datums/proximity_monitor/fields/timestop.dm index 86ea41aee01..c48759c1deb 100644 --- a/code/datums/proximity_monitor/fields/timestop.dm +++ b/code/datums/proximity_monitor/fields/timestop.dm @@ -238,7 +238,7 @@ //you don't look quite right, is something the matter? /datum/proximity_monitor/advanced/timestop/proc/into_the_negative_zone(atom/A) - A.add_atom_colour(list(-1,0,0,0, 0,-1,0,0, 0,0,-1,0, 0,0,0,1, 1,1,1,0), TEMPORARY_COLOUR_PRIORITY) + A.add_atom_colour(COLOR_MATRIX_INVERT, TEMPORARY_COLOUR_PRIORITY) //let's put some colour back into your cheeks /datum/proximity_monitor/advanced/timestop/proc/escape_the_negative_zone(atom/A) diff --git a/code/game/turfs/open/lava.dm b/code/game/turfs/open/lava.dm index e55c998a0d9..1a174723d85 100644 --- a/code/game/turfs/open/lava.dm +++ b/code/game/turfs/open/lava.dm @@ -68,7 +68,7 @@ // But that's rare, and I'm ok with that, quartering our light source count is useful var/mutable_appearance/light_mask = mutable_appearance(mask_icon, mask_state, LIGHTING_MASK_LAYER, src, LIGHTING_PLANE) light_mask.blend_mode = BLEND_MULTIPLY - light_mask.color = list(-1,0,0,0, 0,-1,0,0, 0,0,-1,0, 0,0,0,1, 1,1,1,0) + light_mask.color = COLOR_MATRIX_INVERT . += light_mask /// Refreshes this lava turf's lighting diff --git a/code/modules/admin/view_variables/color_matrix_editor.dm b/code/modules/admin/view_variables/color_matrix_editor.dm index 5ea6d237a6a..7fde4c24590 100644 --- a/code/modules/admin/view_variables/color_matrix_editor.dm +++ b/code/modules/admin/view_variables/color_matrix_editor.dm @@ -12,7 +12,7 @@ else if(istext(_target?.color)) current_color = color_hex2color_matrix(_target.color) else - current_color = color_matrix_identity() + current_color = COLOR_MATRIX_IDENTITY var/mutable_appearance/view = image('icons/misc/colortest.dmi', "colors") if(_target) diff --git a/code/modules/admin/view_variables/get_variables.dm b/code/modules/admin/view_variables/get_variables.dm index c59391371df..d493831da67 100644 --- a/code/modules/admin/view_variables/get_variables.dm +++ b/code/modules/admin/view_variables/get_variables.dm @@ -338,7 +338,7 @@ if(VV_COLOR_MATRIX) .["value"] = open_color_matrix_editor() - if(.["value"] == color_matrix_identity()) //identity is equivalent to null + if(.["value"] == COLOR_MATRIX_IDENTITY) //identity is equivalent to null .["class"] = null if(VV_INFINITY) diff --git a/code/modules/client/client_colour.dm b/code/modules/client/client_colour.dm index 9ac64851a68..c7ff9a02c6e 100644 --- a/code/modules/client/client_colour.dm +++ b/code/modules/client/client_colour.dm @@ -191,7 +191,7 @@ colour = list(/*R*/ 0,0,0,0, /*G*/ 0,175,0,0, /*B*/ 0,0,0,0, /*A*/ 0,0,0,1, /*C*/0,-130,0,0) // Matrix colors /datum/client_colour/monochrome - colour = list(rgb(77,77,77), rgb(150,150,150), rgb(28,28,28), rgb(0,0,0)) + colour = COLOR_MATRIX_GRAYSCALE priority = PRIORITY_HIGH //we can't see colors anyway! override = TRUE fade_in = 20 diff --git a/code/modules/mod/mod_paint.dm b/code/modules/mod/mod_paint.dm index 351859e5f2b..320276cd00d 100644 --- a/code/modules/mod/mod_paint.dm +++ b/code/modules/mod/mod_paint.dm @@ -16,7 +16,7 @@ /obj/item/mod/paint/Initialize(mapload) . = ..() - current_color = color_matrix_identity() + current_color = COLOR_MATRIX_IDENTITY /obj/item/mod/paint/examine(mob/user) . = ..() @@ -66,7 +66,7 @@ . = ..() editing_mod = null QDEL_NULL(proxy_view) - current_color = color_matrix_identity() + current_color = COLOR_MATRIX_IDENTITY /obj/item/mod/paint/ui_status(mob/user) if(check_menu(editing_mod, user))