4

cssで定義されたカラーワードのrgbカラーを取得するJavaScriptプログラムを書きたいと思います。

たとえば、 と入力するとred、 が出力されますrgb(255, 0, 0)rgb(255, 0, 0)また、 からに変換したいと思いredます。

JavaScriptでこれを行う方法はありますか?

4

3 に答える 3

2

あなたは出来る

  • JavaScript を使用して新しい要素を作成します。
  • background-color os スタイルを入力値に設定します
  • 本体に追加します
  • メモを取得: 互換性window.getComputedStyle
  • 同等のbackgroundColorOSを返す

function getRGB(v) {
    var el = document.createElement("div");
    el.style["background-color"] = v;
    document.body.appendChild(el);

    var style = window.getComputedStyle(el);

    var color = style["backgroundColor"];

    document.body.removeChild(el);
    return color;

}

getRGB ("red") //"rgb(255, 0, 0)"

ただし、注意してください:クリストフが言うように、常に正しい値を取得できるとは言えませ
んが、Chromeでは私にとっては非常にうまく機能します

しかし、クリストフが示唆しているように、マップなしで逆に取得できるとは思わない

JSBinのデーモン

アップデート


以下は、色の 16 進数、名前付き、および RGB 表現を含むカラー オブジェクトを返す完全なマップを持つ関数です。

function getColor (r,g,b) {

var colors = {TooBigToPostHere:...}     //see the JSBin                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
function rgbToHex(r, g, b) {
     var hex = "#";
     for (var i = 0; i < 3; i++) {
         var _hex = arguments[i].toString(16);
         while (_hex.length < 2) _hex = "0" + _hex;
         hex += _hex
     }
     return hex.toUpperCase();
 }
 if (typeof r === "string")  r = r["to"+(!!~r.indexOf("#")?"Upper":"Lower")+"Case"]();   
 if (r in colors) return colors[r]
 else if (r !== undefined && g !== undefined && b !== undefined) {
     var hex = rgbToHex(r, g, b);
     if (hex in colors) return colors[hex]
     else return {
             rgb: [r, g, b],
             hex: hex,
             name: null
     }
 } else {
     throw new SyntaxError("Invalid Arguments");
 }

}

次の出力が生成されます。

console.log ( getColor (245,245,245)) //{"hex": "#F5F5F5", "name": "whitesmoke", "rgb": [245, 245, 245]}
console.log ( getColor ("#EE82EE")); //{"hex": "#EE82EE", "name": "violet", "rgb": [238, 130, 238]}
console.log ( getColor ("red")); //{"hex": "#FF0000", "name": "red", "rgb": [255, 0, 0]}

およびJSBinのデモ注: 色には、拡張カラー キーワード
の完全なリストが含まれています。

上記のカラーテーブルをスクレイピングするために使用したコードは次のとおりです

var colors = {};
[].forEach.call(document.querySelectorAll (".colortable tr"), function (e,i) {
if ( i > 0 ) {
 var hex = e.children[3].innerText;
 colors[hex] = {};
 colors[hex].hex = hex;
 colors[hex].name = e.children[2].innerText;
 colors[hex].rgb = e.children[4].innerText.split(",");
 colors[hex].rgb.map(function (a,b,c) {c[b] = +a})
 colors[colors[hex].name] = colors[hex];
 }
});
于 2013-05-29T08:28:14.287 に答える
2

これがあなたが望むものだと思います:

Text:
<input type="text" id="ctext" />
<br>RGB:
<input type="text" id="crgb" />
<br>
<button onclick="doMagic();">Magic</button>
<div id="output" style="display:none"></div>
<script>
    function doMagic() {
        $('#output').html('<p id=test style=\'color:' + $('#ctext').val() + ';\'>sometext</p>');
        $('#crgb').val($('#test').css("color"));
    }
</script>

fiddleで確認してください。

私はそれがうまくいくと思います!

于 2013-05-29T08:33:24.180 に答える