+/**
+ * Convert an RGB color to its nearest quake color.
+ * I'll cheat on this a bit by translating the colors to HSV first,
+ * S and V decide if it's black or white, otherwise, H will decide the
+ * actual color.
+ * @param _r Red (0-255)
+ * @param _g Green (0-255)
+ * @param _b Blue (0-255)
+ * @return A quake color character.
+ */
+static char Sys_Con_NearestColor(const unsigned char _r, const unsigned char _g, const unsigned char _b)
+{
+ float r = ((float)_r)/255.0;
+ float g = ((float)_g)/255.0;
+ float b = ((float)_b)/255.0;
+ float min = min(r, min(g, b));
+ float max = max(r, max(g, b));
+
+ int h; ///< Hue angle [0,360]
+ float s; ///< Saturation [0,1]
+ float v = max; ///< In HSV v == max [0,1]
+
+ if(max == min)
+ s = 0;
+ else
+ s = 1.0 - (min/max);
+
+ // Saturation threshold. We now say 0.2 is the minimum value for a color!
+ if(s < 0.2)
+ {
+ // If the value is less than half, return a black color code.
+ // Otherwise return a white one.
+ if(v < 0.5)
+ return '0';
+ return '7';
+ }
+
+ // Let's get the hue angle to define some colors:
+ if(max == min)
+ h = 0;
+ else if(max == r)
+ h = (int)(60.0 * (g-b)/(max-min))%360;
+ else if(max == g)
+ h = (int)(60.0 * (b-r)/(max-min) + 120);
+ else // if(max == b) redundant check
+ h = (int)(60.0 * (r-g)/(max-min) + 240);
+
+ if(h < 36) // *red* to orange
+ return '1';
+ else if(h < 80) // orange over *yellow* to evilish-bright-green
+ return '3';
+ else if(h < 150) // evilish-bright-green over *green* to ugly bright blue
+ return '2';
+ else if(h < 200) // ugly bright blue over *bright blue* to darkish blue
+ return '5';
+ else if(h < 270) // darkish blue over *dark blue* to cool purple
+ return '4';
+ else if(h < 330) // cool purple over *purple* to ugly swiny red
+ return '6';
+ else // ugly red to red closes the circly
+ return '1';
+}
+