4
A
回答
1
首先負載http://www.phpied.com/files/rgbcolor/rgbcolor.js
然後,你可以做
$.fn.invertElement = function() {
var prop = 'color';
if (!this.css(prop)) return;
var color = new RGBColor(this.css(prop));
if (color.ok) {
this.css(prop, 'rgb(' + (255 - color.r) + ',' + (255 - color.g) + ',' + (255 - color.b) + ')');
}
};
$('div').invertElement();
當用字(如「黑」)中指定的顏色屬性這也應該工作,而不是一個RGB值。但是,透明度不好。
9
有點晚,但遲到總比不到好:
function invert(rgb) {
rgb = Array.prototype.join.call(arguments).match(/(-?[0-9\.]+)/g);
for (var i = 0; i < rgb.length; i++) {
rgb[i] = (i === 3 ? 1 : 255) - rgb[i];
}
return rgb;
}
console.log(
invert('rgba(255, 0, 0, 0.3)'), // 0, 255, 255, 0.7
invert('rgb(255, 0, 0)'), // 0, 255, 255
invert('255, 0, 0'), // 0, 255, 255
invert(255, 0, 0) // 0, 255, 255
);
+0
謝謝!簡單而有用! 將是真棒有一個函數,反轉十六進制和rgb沒有metter作爲參數傳遞 – 2013-11-12 19:38:33
1
我發現了一個巨大的 '十六進制顏色變頻' 功能中寫道馬特LaGrandeur(http://www.mattlag.com/)
function invertHex(hexnum){
if(hexnum.length != 6) {
console.error("Hex color must be six hex numbers in length.");
return false;
}
hexnum = hexnum.toUpperCase();
var splitnum = hexnum.split("");
var resultnum = "";
var simplenum = "FEDCBA9876".split("");
var complexnum = new Array();
complexnum.A = "5";
complexnum.B = "4";
complexnum.C = "3";
complexnum.D = "2";
complexnum.E = "1";
complexnum.F = "0";
for(i=0; i<6; i++){
if(!isNaN(splitnum[i])) {
resultnum += simplenum[splitnum[i]];
} else if(complexnum[splitnum[i]]){
resultnum += complexnum[splitnum[i]];
} else {
console.error("Hex colors must only include hex numbers 0-9, and A-F");
return false;
}
}
return resultnum;
}
來源是在這裏: http://www.mattlag.com/scripting/hexcolorinverter.php
相關問題
- 1. jQuery:更改特定的文本顏色
- 2. PyQt給特定元素的顏色
- 3. 如何使用ColorStateList更改特定元素BottomNavigationView的顏色?
- 4. 反轉顏色
- 5. 在CCLabelTTF中使用Cocos2d中的glBlendFunc反轉文本顏色
- 6. RichTextBox的文本顏色的特定行
- 7. 使用jQuery查找具有特定文本(值)的子元素
- 8. 使用jQuery中的特定文本搜索元素
- 9. 如何使用Jquery選擇此特定td元素的文本
- 10. 使用jquery改變文本的顏色
- 11. UITableView,通過使用indexpath.row == ...顏色特定單元格的文本,顏色超過一個單元格
- 12. 使用jQuery更改選項元素中文本部分的顏色
- 13. 使用jQuery的目標特定元素
- 14. 不使用範圍的特定文本的顏色
- 15. 將顏色應用於C中數組中的特定元素
- 16. 更改個別元素的選定文本顏色
- 17. 如何使用javascript更改特定文本的顏色
- 18. 使用ColorMatrixColorFilter反轉顏色(負)
- 19. 使用jquery將文本顏色更改爲背景顏色
- 20. jQuery獲取元素的顏色?
- 21. 如何反轉UIView特定區域的顏色?
- 22. 反轉文本顏色取決於背景色
- 23. 在C中反轉BMP文件中的像素顏色
- 24. jquery,js。自定義文本顏色
- 25. 使用jQuery查找特定元素
- 26. 使用jQuery導航到特定元素
- 27. 使用jQuery選擇特定元素
- 28. 使用jQuery刪除所有元素的背景顏色/圖像
- 29. 使用jQuery根據動態值更改元素的顏色
- 30. jQuery使用通配符*改變所有子元素的顏色
簡而言之,獲取顏色,將其轉化爲3個十進制值(它以十六進制IIRC形式返回)並從255中減去這些值。然後,您有你的R,G和B頻道,你使用'。.css()'來再次應用它們。 – Bojangles 2012-02-01 18:33:17
http://webhole.net/2010/01/06/how-to-invert-an-elements-color/ – 2012-02-01 18:33:41
http://stackoverflow.com/questions/4766201/javascript-invert-color-on-all-元素一頁 – j08691 2012-02-01 18:36:13