Less.js Misc color() Function
Last Updated :
06 Nov, 2022
Less.js is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. Since CSS is a dynamic style sheet language, it is preferred. LESS is adaptable, so it works with a wide range of browsers. Only CSS that has been created and processed using the CSS pre-processor, a computer language, can be used by web browsers. In addition to providing capabilities like variables, functions, mixins, and operations to help create dynamic CSS while maintaining backward compatibility, it is a CSS language extension.
In this article, we are going to discuss the Misc color() function, whose function is to transform a string value into a color object. This function returns the color object value and this can only take hex color code, not HSL or RGB values.
Syntax:
color("string")
Parameters:
- string: This is the only compulsory parameter for this function. This parameter takes the string value of the name of the color mainly a hex color code.
Please refer to the Compile LESS code into CSS code. article for the detailed description.
Example 1: The code below demonstrates the usage and implementation of the Misc color() function along with if and boolean functions.
HTML
<!DOCTYPE html>
< html >
< head >
< link rel = "stylesheet"
type = "text/css"
href = "style.css" />
</ head >
< body >
< h1 style = "color:green" >GeeksforGeeks</ h1 >
< h3 >Less.js Misc color() Function</ h3 >
< div class = "c1" >
< p >boolean(iscolor("#21ff7a"))
< br >TRUE
</ p >
</ div >
</ body >
</ html >
|
style.less:
CSS
@body-bg- color : #eeeeee ;
@str: "#21ff7a" ;
@color 1: color(@str);
@color 2: #381508 ;
@cond: boolean(iscolor(@color 1 ));
body {
background-color : @body-bg-color;
}
.c 1 {
width : 500px ;
height : 250px ;
background-color : if(@cond, @color 1 , @color 2 );
margin : 1 rem;
}
p {
color : if(@cond, @color 2 , @color 1 );
font-size : 1.5 rem;
padding : 5 rem 0 0 4.5 rem;
}
|
Now, to compile the above LESS code to CSS code, run the following command:
lessc style.less style.css
style.css: The compiled CSS file comes to be:
CSS
body {
background-color : #eeeeee ;
}
.c 1 {
width : 500px ;
height : 250px ;
background-color : #21ff7a ;
margin : 1 rem;
}
p {
color : #381508 ;
font-size : 1.5 rem;
padding : 5 rem 0 0 4.5 rem;
}
|
Output:
Example 2: The code below demonstrates the usage and implementation of the Misc color() function along with using saturate color operation functions with if and boolean functions.
HTML
<!DOCTYPE html>
< html >
< head >
< link rel = "stylesheet"
type = "text/css"
href = "style.css" />
</ head >
< body >
< h1 style = "color:green" >GeeksforGeeks</ h1 >
< h3 >Less.js Misc color() Function</ h3 >
< div class = "c1" >
< p >saturate(#80e619, 50%)
< br >boolean(iscolor("#80ff00"))
< br >TRUE
</ p >
</ div >
</ body >
</ html >
|
style.less:
CSS
@body-bg- color : #eeeeee ;
@str: "#80e619" ;
@color 1: color(@str);
@color 2: #381508 ;
@color 3: saturate(@color 1 , 50% );
@cond: boolean(iscolor(@color 3 ));
body {
background-color : @body-bg-color;
}
.c 1 {
width : 500px ;
height : 250px ;
background-color : if(@cond, @color 3 , @color 2 );
margin : 1 rem;
}
p {
color : if(@cond, @color 2 , @color 1 );
font-size : 1.5 rem;
padding : 5 rem 0 0 4.5 rem;
}
|
Now, to compile the above LESS code to CSS code, run the following command:
lessc style.less style.css
style.css: The compiled CSS file comes to be:
CSS
body {
background-color : #eeeeee ;
}
.c 1 {
width : 500px ;
height : 250px ;
background-color : #80ff00 ;
margin : 1 rem;
}
p {
color : #381508 ;
font-size : 1.5 rem;
padding : 5 rem 0 0 4.5 rem;
}
|
Output:
Reference: https://lesscss.org/functions/#misc-functions-color
Share your thoughts in the comments
Please Login to comment...