2016-03-25 13 views
1

Şimdi 'Temizle' adlı bir renk içeren bir nesne alıyorum. Bunu RGB değerine ayarlamak istiyorum ve bunu yapmak için bir işlev kullandım, ama bana 'rgb (51, 51, 51)' geri döndü, açısal renk şemasında ise '# 3366cc''e geri dönebilirim. Diğer js kütüphanelerini kullanarak rgb değerini elde edersiniz? -Renk "Net" RGB veya HEX'e nasıl ayarlanır?


function(){ 
    var div = document.createElement('div'); 
    var rgbColor; 
    div.style.color = 'Clear'; 
    document.body.appendChild(div); 
    rgbColor = window.getComputedStyle(div).color 
    div.remove(); 
    return rgbColor; 
} 
+0

http://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb – Sagi

cevap

0
function componentToHex(c) { 
    var hex = c.toString(16); 
    return hex.length == 1 ? "0" + hex : hex; 
} 

function rgbToHex(r, g, b) { 
    return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b); 
} 

function getColor(){ 
    var r = 0; 
    var g = 0; 
    var b = 0; 
    var rgbColor; 
    var div = document.createElement('div'); 

    div.style.color = 'Clear'; 
    document.body.appendChild(div); 
    rgbColor = window.getComputedStyle(div).color 
    var matches = div.style.color.match(/^rgb\((\d+), (\d+), (\d+)\)$/); 

    if (matches) { 
     r = parseInt(matches[1]); 
     g = parseInt(matches[2]); 
     b = parseInt(matches[3]); 
    } 
    div.remove(); 
    return rgbToHex(r, g, b); 
}