You are here: Reference > JavaScript > client-side > style handling > methods > getRGBColorValue (CSSPrimitiveValue)

getRGBColorValue method (CSSPrimitiveValue)

Browser support:
If the value represented by the current CSSPrimitiveValue object is a color, then retrieves an RGBColor object that represents the value.
This method can be used if the type of the represented value is CSS_RGBCOLOR. To get the type of the represented value, use the primitiveType property.


object.getRGBColorValue ( );
You can find the related objects in the Supported by objects section below.

Return value:

Returns an RGBColor object that represents the value.

Example HTML code 1:

This example illustrates the use of the getRGBColorValue method:
    <script type="text/javascript">
        function GetRGBComponents () {
            var div = document.getElementById ("myDiv");
            if (window.getComputedStyle) {
                var compStyle = window.getComputedStyle (div, null);
                try {
                    var value = compStyle.getPropertyCSSValue ("background-color");
                    var valueType = value.primitiveType;
                    if (valueType == CSSPrimitiveValue.CSS_RGBCOLOR) {
                        var rgb = value.getRGBColorValue ();
                        var r = (CSSPrimitiveValue.CSS_NUMBER);
                        var g = (CSSPrimitiveValue.CSS_NUMBER);
                        var b = (CSSPrimitiveValue.CSS_NUMBER);

                        var message = "red: " + r + "\n";
                        message += "green: " + g + "\n";
                        message += "blue: " + b;
                        alert (message);
                    else {
                        alert ("The value of the background-color property is not an RGB function!");
                catch (e) {
                    alert ("Your browser does not support the getPropertyCSSValue method!");
            else {
                alert ("Your browser does not support the getComputedStyle method!");
    <div id="myDiv" style="background-color:yellow;">An element with background-color.</div>
    <br />
    Use the following button to get the red, green and blue components of the color specified by the background-color property!
    <button onclick="GetRGBComponents ()">Get RGB components</button>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content