Snippet for RGB to Hex

Mods, move this if this is the wrong section. This definitely isn't a plugin on it's own but wanted to share it anyways.

Since MV now uses Hex values for colors, here's a snippet that can convert RGB to Hex. Makes things friendlier for devs and end users.

Code:
function toHex(n) {    n = parseInt(n,10);    if (isNaN(n)) return "00";    n = Math.max(0,Math.min(n,255));    return "0123456789ABCDEF".charAt((n-n%16)/16)        + "0123456789ABCDEF".charAt(n%16);};	function rgbToHex(rgb) {    nrgb = rgb.replace(/\s+/g, '');    nrgb = nrgb.split(',');    return ("#" + toHex(nrgb[0]) + toHex(nrgb[1]) + toHex(nrgb[2]));};
Usage:
Code:
var rgb_value = "255, 0, 255"; // Uses string format for plugin propertiesvar hex_value = rgbToHex(rgb_value) // Outputs #FF00FF
Really?  Where do you see it using hex?  Everything I've looked at uses individual RGB with 0-255.
Document for bitmap.textColor says "The color of the text in CSS format." So I had assumed it was all just in hex. Never actually noticed it in the core scripts, but either way it works. I actually completely forgot you can use rgb( r, g, b ) in css. It's been awhile since I've done anything web related. Sorry about the junk post then.


EDIT: Yup, looked through core scripts. I feel silly now. Also found a method in the Utils class. "rgbToCssColor". Well if anyone wants hex, here it is. xD
still good to have 🙂

Original thread: https://forums.rpgmakerweb.com/threads/snippet-for-rgb-to-hex.47143/