steven
steven

Reputation: 71

rgb value as variable

How do I store a variable value in a rgb() ? I use this code which isn't working:

<script>
var R=200;
var colval="rgb(R,10,100)";
</script>

I want it to be like this:

<script>
var colval="rgb(200,10,100)";
</script>

but somehow it doesn't store R right , putting quotes around 200 or R isn't working either.

Upvotes: 7

Views: 15282

Answers (3)

user19804538
user19804538

Reputation:

Now you could use template string:

    <script>
        var R = 200;
        var colval = `rgb(${R}, 10, 100)`;
    </script>

    

Upvotes: 0

pr0mpT_07
pr0mpT_07

Reputation: 184

Use the "+" because in javascript the is the operator used to concatenate, or add things together to make a string. In this case we are adding together "rgb("+ r +","+ g +","+ b +")" to make a string that looks like "rgb(225, 34, 154)". When the browser's JS interpreter reads this code, "rgb("+ r +","+ g +","+ b +")", it will replace the r, g, and b with the values of those variables.

Upvotes: 0

alexn
alexn

Reputation: 59012

I assume you're using JavaScript:

<script>
    var R = 200;
    var colval = "rgb(" + R + ",10,100)";
</script>

Results in colval = rgb(200,10,100)

Upvotes: 8

Related Questions