﻿function getHexColor(value)
{
     if (value.indexOf("rgb(") != -1)
     {
         value = value.replace("rgb(", "");
         value = value.replace(")", "");
         value = value.replace(" ", "");

         var R = value.split(",")[0];
         var G = value.split(",")[1];
         var B = value.split(",")[2];

         return "#" + RGBtoHex(R, G, B);
     }
     else
     {
         return value;
     }
}

function RGBtoHex(R, G, B)
{
    return toHex(R) + toHex(G) + toHex(B) 
}
function toHex(N)
{
    if (N == null) return "00";
    N = parseInt(N); if (N == 0 || isNaN(N)) return "00";
    N = Math.max(0, N); N = Math.min(N, 255); N = Math.round(N);
    
    return "0123456789ABCDEF".charAt((N - N % 16) / 16) + "0123456789ABCDEF".charAt(N % 16);
}