Files
Yogstation/code/__HELPERS/sanitize_values.dm
Chubbygummibear a797efa567 TG DNA update attempt #3: post-TGUI preferences edition (#18197)
* please let this work (#1)

* ho

* pod nerfed by a pod main

* sugone

* yea

* yer

* lotsa stuff

* tgui is pain

* i don't know

* maybe?

* god kill me

* tailed

* wait no i fixed it wrong, this should be better

* SCOPE CREEP BABEEEEEEEEEE

* sins

* mother of all omlettes jack

* is it over?

* ethereal colors because idk

* i think that's it until we find out what else i broke

cl ass

clean up debug messages

last one probably

* Revert "i think that's it until we find out what else i broke"

This reverts commit 574b0e8b75.

* last time with gusto

* hhh

* ho

* pod nerfed by a pod main

* yer

* fix

* Revert "fix"

This reverts commit 979eddbaa8889a249cc9ff830109757af888e753.

* Delete plantpeople.dm

* Revert "Merge branch 'dna-blocks-but-like-good' of https://github.com/Chubbygummibear/Yogstation-TG into dna-blocks-but-like-good"

This reverts commit 1756ff2be6, reversing
changes made to 05f88bee81.

* this shouldn't be in here

* missed that

* so many little things

* please for the love of god work

* that might be it

* may the future not suffer as we have in the past

* higher version because it only updates sometimes

* works

* reorder those cause yog is snowflake

* updated

* conflicting mutations fix

* alright we're good to go AGAIN

* glowy and extendo colors work again

* please let this work (#1)

* ho

* pod nerfed by a pod main

* sugone

* yea

* yer

* lotsa stuff

* tgui is pain

* i don't know

* maybe?

* god kill me

* tailed

* wait no i fixed it wrong, this should be better

* SCOPE CREEP BABEEEEEEEEEE

* sins

* mother of all omlettes jack

* is it over?

* ethereal colors because idk

* i think that's it until we find out what else i broke

cl ass

clean up debug messages

last one probably

* Revert "i think that's it until we find out what else i broke"

This reverts commit 574b0e8b75.

* last time with gusto

* hhh

* ho

* pod nerfed by a pod main

* yer

* fix

* Revert "fix"

This reverts commit 979eddbaa8889a249cc9ff830109757af888e753.

* Delete plantpeople.dm

* Revert "Merge branch 'dna-blocks-but-like-good' of https://github.com/Chubbygummibear/Yogstation-TG into dna-blocks-but-like-good"

This reverts commit 1756ff2be6, reversing
changes made to 05f88bee81.

* this shouldn't be in here

* missed that

* so many little things

* please for the love of god work

* that might be it

* may the future not suffer as we have in the past

* works

* reorder those cause yog is snowflake

* updated

* conflicting mutations fix

* alright we're good to go AGAIN

* glowy and extendo colors work again

* it compiles

* the adds antags pr is the worst thing i've ever seen

* Revert "the adds antags pr is the worst thing i've ever seen"

This reverts commit b18dc91a34.

* i need github to stop being a shit for 2 seconds

* i didn't even need those god dammit

* can i go now
2023-08-01 17:16:14 -05:00

120 lines
4.0 KiB
Plaintext

//general stuff
/proc/sanitize_integer(number, min=0, max=1, default=0)
if(isnum(number))
number = round(number)
if(min <= number && number <= max)
return number
return default
/proc/sanitize_float(number, min=0, max=1, accuracy=1, default=0)
if(isnum(number))
number = round(number, accuracy)
if(min <= number && number <= max)
return number
return default
/proc/sanitize_text(text, default="")
if(istext(text))
return text
return default
/proc/sanitize_islist(value, default)
if(islist(value) && length(value))
return value
if(default)
return default
/proc/sanitize_inlist(value, list/List, default)
if(value in List)
return value
if(default)
return default
if(List?.len)
return pick(List)
//more specialised stuff
/proc/sanitize_gender(gender,neuter=0,plural=1, default="male")
switch(gender)
if(MALE, FEMALE)
return gender
if(NEUTER)
if(neuter)
return gender
else
return default
if(PLURAL)
if(plural)
return gender
else
return default
return default
/**
* Sanitize_Hexcolor takes in a color in hexcode as a string, be it shorthand hex as 3 characters, or full-sized 6 digit hex, with or without a leading #
* you can pass it a full hexcode with leading #, such as "#FFFFFF", and with the default arguments you will get exactly that color back, because it accounts for
* leading # signs and ignores them, then later in the function will either readd one, or won't depending on what you want.
*
* Full hexcolors will just be validated, shorthand hex of 3 characters can be cleanly converted up to full hex with a leading # no problem.
*
* With default arguments:
* * "FFF" -> "#FFFFFF"
* * "#FFFFFF" -> "#FFFFFF"
*
* converting down to short, with or without the # is doable by setting the desired format to the length you want and specifying the crunch to true for adding a # or false to not
*/
/proc/sanitize_hexcolor(color, desired_format = DEFAULT_HEX_COLOR_LEN, include_crunch = TRUE, default)
var/crunch = include_crunch ? "#" : ""
if(!istext(color))
color = ""
//start checks for a leading "#", and if there is it skips past it before doing the color logic.
//this means you can pass something like "#FFFFFF" into sanitize_hexcolor without arguments, and it will automatically cut the leading #, and readd it at the end
//there is no risk of accidentally creating a malformed color like ##FFFFFF
var/start = 1 + (text2ascii(color, 1) == 35)
var/len = length(color)
var/char = ""
// Used for conversion between RGBA hex formats.
var/format_input_ratio = "[desired_format]:[length_char(color)-(start-1)]"
. = ""
var/i = start
while(i <= len)
char = color[i]
i += length(char)
switch(text2ascii(char))
if(48 to 57) //numbers 0 to 9
. += char
if(97 to 102) //letters a to f
. += char
if(65 to 70) //letters A to F
char = lowertext(char)
. += char
else
break
switch(format_input_ratio)
//if you're trying to convert up from short hex (3 characters) to a full hex 6, that's what these switch statements are doing, adding and removing to meet the desired format
if("3:8", "4:8", "3:6", "4:6") //skip next one. RRGGBB(AA) -> RGB(A)
i += length(color[i])
if("6:4", "6:3", "8:4", "8:3") //add current char again. RGB(A) -> RRGGBB(AA)
. += char
if(length_char(.) == desired_format)
return crunch + .
switch(format_input_ratio) //add or remove alpha channel depending on desired format.
if("3:8", "3:4", "6:4")
return crunch + copytext(., 1, desired_format+1)
if("4:6", "4:3", "8:3")
return crunch + . + ((desired_format == 4) ? "f" : "ff")
else //not a supported hex color format.
return default ? default : crunch + repeat_string(desired_format, "0")
/proc/sanitize_color(color)
if(length(color) != length_char(color))
CRASH("Invalid characters in color '[color]'")
var/list/HSL = rgb2hsl(hex2num(copytext(color, 2, 4)), hex2num(copytext(color, 4, 6)), hex2num(copytext(color, 6, 8)))
HSL[3] = min(HSL[3],0.4)
var/list/RGB = hsl2rgb(arglist(HSL))
return "#[num2hex(RGB[1],2)][num2hex(RGB[2],2)][num2hex(RGB[3],2)]"