Skip to content

Commit

Permalink
[MODULAR] Chat color tweaks! (#323) (#1512)
Browse files Browse the repository at this point in the history
* Chat color tweaks, v1

* Update chat_color.dm

* Update chat_color.dm

* Update chat_color.dm

* Update chat_color.dm

* Whoops

* Whoops again!

* Update chat_color.dm

* Uhmm this doesn't include the # apparently

* Just making this a bit neater and adding comments

was it worth it?

* Chat colors now go off your voice rather than name

Co-authored-by: Bloop <[email protected]>
  • Loading branch information
Steals-The-PRs and vinylspiders authored Jan 11, 2024
1 parent 6250f90 commit efbd827
Showing 1 changed file with 53 additions and 26 deletions.
79 changes: 53 additions & 26 deletions modular_nova/modules/chat_colors/code/chat_color.dm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
return process_chat_color(sanitize_hexcolor(input))

/datum/preference/color/chat_color/create_default_value()
return process_chat_color(random_color())
return process_chat_color("#[random_color()]")

/datum/preference/color/chat_color/serialize(input)
return process_chat_color(sanitize_hexcolor(input))
Expand Down Expand Up @@ -41,14 +41,23 @@

#undef CHAT_COLOR_NORMAL
#undef CHAT_COLOR_DARKENED
#define CM_COLOR_SAT_MIN 0
#define CM_COLOR_SAT_MAX 1
#define CM_COLOR_LUM_MIN 0.35
#define CM_COLOR_LUM_MAX 1

#define CM_COLOR_HUE 1
#define CM_COLOR_SATURATION 2
#define CM_COLOR_LUMINANCE 3

#define CM_COLOR_SAT_MAX 90 // 90% saturation is the default ceiling
#define CM_COLOR_LUM_MIN 40 // 40% luminosity is the default floor
#define CM_COLOR_LUM_MIN_GREY 35 // 35% luminosity for greys
#define CM_COLOR_LUM_MAX_DARK_RANGE 45 // 45% luminosity for dark blues/reds/violets

#define CM_COLOR_HUE_RANGE_LOWER 180
#define CM_COLOR_HUE_RANGE_UPPER 350
#define CM_COLOR_HUE_GREY 0

/**
* Converts a given color to comply within a smaller subset of colors to be used in runechat.
* If a color is outside the min/max saturation or value/lum, it will be set at the nearest
* If a color is outside the min/max saturation or lum, it will be set at the nearest
* value that passes validation.
*
* Arguments:
Expand All @@ -58,24 +67,42 @@
*/
/proc/process_chat_color(color, sat_shift = 1, lum_shift = 1)
if(isnull(color))
return

var/input_hsv = RGBtoHSV(color)
var/list/split_hsv = ReadHSV(input_hsv)
var/split_h = split_hsv[1]
var/split_s = split_hsv[2]
var/split_v = split_hsv[3]
var/processed_s = clamp(split_s, CM_COLOR_SAT_MIN * 255, CM_COLOR_SAT_MAX * 255)
var/processed_v = clamp(split_v, CM_COLOR_LUM_MIN * 255, CM_COLOR_LUM_MAX * 255)
// adjust for shifts
processed_s *= clamp(sat_shift, 0, 1)
processed_v *= clamp(lum_shift, 0, 1)
var/processed_hsv = hsv(split_h, processed_s, processed_v)
var/processed_rgb = HSVtoRGB(processed_hsv)

return processed_rgb

#undef CM_COLOR_LUM_MAX
#undef CM_COLOR_LUM_MIN
return "#FFFFFF"

// Convert color hex to HSL
var/hsl_color = rgb2num(color, COLORSPACE_HSL)

// Hue / saturation / luminance
var/hue = hsl_color[CM_COLOR_HUE]
var/saturation = hsl_color[CM_COLOR_SATURATION]
var/luminance = hsl_color[CM_COLOR_LUMINANCE]

// Cap the saturation at 90%
saturation = min(saturation, CM_COLOR_SAT_MAX)

// Now clamp the luminance according to the hue
var/processed_luminance

// There are special cases for greyscale and the red/blue/violet range
if(hue == CM_COLOR_HUE_GREY)
processed_luminance = max(luminance, CM_COLOR_LUM_MIN_GREY) // greys have a higher floor on the allowed luminance value
else if(CM_COLOR_HUE_RANGE_UPPER > hue > CM_COLOR_HUE_RANGE_LOWER)
processed_luminance = min(luminance, CM_COLOR_LUM_MAX_DARK_RANGE) // colors in the deep reds/blues/violets range will have a slightly higher luminance floor than the rest
else
processed_luminance = max(luminance, CM_COLOR_LUM_MIN) // everything else gets the default

// Convert it back to a hex
return rgb(hue, saturation*sat_shift, processed_luminance*lum_shift, space = COLORSPACE_HSL)

#undef CM_COLOR_HUE
#undef CM_COLOR_SATURATION
#undef CM_COLOR_LUMINANCE

#undef CM_COLOR_SAT_MAX
#undef CM_COLOR_SAT_MIN
#undef CM_COLOR_LUM_MIN
#undef CM_COLOR_LUM_MIN_GREY
#undef CM_COLOR_LUM_MAX_DARK_RANGE

#undef CM_COLOR_HUE_RANGE_LOWER
#undef CM_COLOR_HUE_RANGE_UPPER
#undef CM_COLOR_HUE_GREY

0 comments on commit efbd827

Please sign in to comment.