[MIRROR] Color matrix defines for filters and identity [MDB IGNORE] (#25823)

* 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.

* Color matrix defines for filters and identity

---------

Co-authored-by: Ghom <42542238+Ghommie@users.noreply.github.com>
This commit is contained in:
SkyratBot
2023-12-24 21:58:26 +01:00
committed by GitHub
parent 4102ead7a6
commit 0f1761fa40
9 changed files with 40 additions and 31 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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