Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give hsl color value to an svg element

Tags:

javascript

svg

I want to ask that If have read that it is possible to give hsl color value to an svg element.But when I try it like this

var color = hsl(0, 100%, 50%);

circle.attr("fill" , color);

I got an error "unexpected number"

Can any one guide what is the correct way and also does svg supports all hsl colors

Thanks

like image 628
A_user Avatar asked Jun 29 '12 06:06

A_user


People also ask

Can I use HSL in an SVG?

SVG Color Codes To specify an SVG color, you can take Color Names, RGB or RGBA values, HEX values, HSL or HSLA values.

What is HSL Colour code?

HSL stands for hue, saturation, and lightness. HSLA color values are an extension of HSL with an Alpha channel (opacity).

What is HSL value in HTML?

HSL stands for hue, saturation, and lightness - and represents a cylindrical-coordinate representation of colors. Hue is a degree on the color wheel (from 0 to 360) - 0 (or 360) is red, 120 is green, 240 is blue. Saturation is a percentage value; 0% means a shade of gray and 100% is the full color.


1 Answers

Simply

var color = hsl(0,100%,50%);

line means, that you call hsl function with 0 ,100%, 50% parameters, and assign returned value to color variable.

You need to use quotes, and pass color as a string:

 var color = "hsl(0, 100%, 50%)";

 circle.attr("fill" , color);

For more information about HSL, read here.

And it doesn't metter whether the hsl color is used by SVG, or by other element. It's just a syntax for specifing color.

UPDATE: To give color variable random behavior, try something like this:

var color = "hsl("+[0,0,0].map(function(){   return Math.round(100*Math.random())+"%"; }).join(',')+")";
like image 138
Engineer Avatar answered Sep 20 '22 08:09

Engineer