Skip to content

Instantly share code, notes, and snippets.

@yangg
Created December 18, 2011 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yangg/1492273 to your computer and use it in GitHub Desktop.
Save yangg/1492273 to your computer and use it in GitHub Desktop.
CSSPrimitiveValue & RGBColor
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8" />
<title>CSSPrimitiveValue & RGBColor</title>
</head>
<body>
<div id="panel1" style="background-color:#09c;"> test</div>
</body>
<script>
var panel = document.getElementById('panel1');
if(window.getComputedStyle) {
var computedStyle = window.getComputedStyle(panel, null);
console.log(computedStyle.getPropertyValue('background-color'));
var color = computedStyle.getPropertyCSSValue('background-color');
console.log(color); // CSSPrimitiveValue
if(color.primitiveType == CSSPrimitiveValue.CSS_RGBCOLOR) {
var rgb = color.getRGBColorValue(); // RGBColor
console.log(rgb);
var r = rgb.red.getFloatValue(CSSPrimitiveValue.CSS_NUMBER);
var g = rgb.green.getFloatValue(CSSPrimitiveValue.CSS_NUMBER);
var b = rgb.blue.getFloatValue(CSSPrimitiveValue.CSS_NUMBER);
console.log(r, g, b);
}
}
// http://www.w3.org/TR/2000/REC-DOM-Level-2-Style-20001113/css.html#CSS-CSSPrimitiveValue
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment