D3.js interpolateBrBG() Function

The d3.interpolateBrBG() function in D3.js is a diverging color scheme that is available as a continuous interpolator. This function returns the corresponding color from the BrBG diverging color scheme.

Syntax:

d3.interpolateBrBG(t);

Parameters: This function accepts a single parameter as mentioned above and described below.

  • t: It is a number in a range [0,1] 

Return Values: It returns an RGB string.

Below given are a few examples of the function given above.



Example1:

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta
            name="viewport"
            path1tent="width=device-width, 
                       initial-scale=1.0"/>
        <title>D3.js interpolateBrBG() Function</title>
    </head>
    <style></style>
    <body>
        <script src=
        </script>
        <script src=
        </script>
        <script src=
        </script>
        <script src=
        </script>
        <script>
            console.log(d3.interpolateBrBG(0));
            console.log(d3.interpolateBrBG(0.1));
            console.log(d3.interpolateBrBG(0.001));
            console.log(d3.interpolateBrBG(0.2));
            console.log(d3.interpolateBrBG(0.211));
            console.log(d3.interpolateBrBG(0.3));
            console.log(d3.interpolateBrBG(1));
        </script>
    </body>
</html>

chevron_right


Output:

Example2:

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta
            name="viewport"
            content="width=device-width, 
                     initial-scale=1.0"/>
        <title>D3.js interpolateBrBG() Function</title>
    </head>
    <style>
        div {
            padding: 10px;
            width: fit-content;
            height: 100px;
            float: left;
        }
    </style>
    <body>
        D3.interpolateBrBG()
        <br />
        <br />
        <div class="b1">
            <span>
                D3.interpolateBrBG(0.9)
            </span>
        </div>
        <div class="b2">
            <span>
                D3.interpolateBrBG(0.1)
            </span>
        </div>
        <div class="b3">
            <span>
                D3.interpolateBrBG(0.2)
            </span>
        </div>
        <div class="b4">
            <span>
                D3.interpolateBrBG(0.5)
            </span>
        </div>
        <!--Fetching from CDN of D3.js -->
        <script src=
        </script>
        <script src=
        </script>
        <script src=
        </script>
        <script src=
        </script>
        <script>
            // Array of colors is given
            let color1 = d3.interpolateBrBG(0.9);
  
            let color2 = d3.interpolateBrBG(0.1);
            let color3 = d3.interpolateBrBG(0.2);
            let color4 = d3.interpolateBrBG(0.5);
  
            let b1 = document.querySelector(".b1");
            let b2 = document.querySelector(".b2");
            let b3 = document.querySelector(".b3");
            let b4 = document.querySelector(".b4");
            b1.style.backgroundColor = color1;
            b2.style.backgroundColor = color2;
            b3.style.backgroundColor = color3;
            b4.style.backgroundColor = color4;
        </script>
    </body>
</html>

chevron_right


Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.