I have this
$colors:
"dark" #3E3E3E,
"darker" #3E3E3E,
"light" #ECECF0,
"green" #00A87B,
"yellow" #FFBB3B,
"red" #FF4633,
"white" #FFFFFF,
"black" #000000,
"blue" #436FB6
;
#each $color in $colors{
// .btn-color
.btn-#{nth($color, 1)}{
background: nth($color, 2);
color: #fff;
}
// .color / color
.#{nth($color, 1)}, #{nth($color, 1)}{
color: nth($color, 2) !important;
a{
color: nth($color, 2);
}
}
// .color-bg
.#{nth($color, 1)}-bg {
background: nth($color, 2);
}
}
It dynamically creates various classes. How can I extend this to make variables for each $colors example — i need to use $dark to reach the dark value from $colors
If you are going to automate color variations I recommend you to take a look into the native color functions provided by Sass (http://sass-lang.com/documentation/Sass/Script/Functions.html).
Here is an example based on a color map (IMO simpler to work with compared to list):
$colors:(
green : #00A87B,
yellow : #FFBB3B,
red : #FF4633,
blue : #436FB6,
white : #FFFFFF,
black : #000000
);
#each $name, $color in $colors {
.color {
&-#{$name}{
background-color: $color;
// create variations (if not black or white)
#if $color != white and $color != black {
&.extra-light { background-color: mix($color, white, 25%); }
&.light { background-color: mix($color, white, 50%); }
&.semi-light { background-color: mix($color, white, 75%); }
&.extra-dark { background-color: mix($color, black, 25%); }
&.dark { background-color: mix($color, black, 50%); }
&.semi-dark { background-color: mix($color, black, 75%); }
}
}
}
}
CSS output
.color-green { background-color: #00A87B;}
.color-green.extra-light { background-color: #bfe9de;}
.color-green.light { background-color: #80d4bd;}
.color-green.semi-light { background-color: #40be9c;}
.color-green.extra-dark { background-color: #002a1f;}
.color-green.dark { background-color: #00543e;}
.color-green.semi-dark { background-color: #007e5c;}
.color-yellow { background-color: #FFBB3B;}
.color-yellow.extra-light { background-color: #ffeece;}
.color-yellow.light { background-color: #ffdd9d;}
.color-yellow.semi-light { background-color: #ffcc6c;}
.color-yellow.extra-dark { background-color: #402f0f;}
.color-yellow.dark { background-color: #805e1e;}
.color-yellow.semi-dark { background-color: #bf8c2c;}
.color-red { background-color: #FF4633;}
.color-red.extra-light { background-color: #ffd1cc;}
.color-red.light { background-color: #ffa399;}
.color-red.semi-light { background-color: #ff7466;}
.color-red.extra-dark { background-color: #40120d;}
.color-red.dark { background-color: #80231a;}
.color-red.semi-dark { background-color: #bf3526;}
.color-blue { background-color: #436FB6;}
.color-blue.extra-light { background-color: #d0dbed;}
.color-blue.light { background-color: #a1b7db;}
.color-blue.semi-light { background-color: #7293c8;}
.color-blue.extra-dark { background-color: #111c2e;}
.color-blue.dark { background-color: #22385b;}
.color-blue.semi-dark { background-color: #325389;}
.color-white { background-color: #FFFFFF;}
.color-black { background-color: #000000;}
Function based approach:
$colors:(
green : #00A87B,
yellow : #FFBB3B,
red : #FF4633,
blue : #436FB6,
white : #FFFFFF,
black : #000000
);
#function get-color($color, $variant: null){
$color: map-get($colors, $color);
#return map-get((
extra-light: mix($color, white, 25%),
light: mix($color, white, 50%),
semi-light: mix($color, white, 75%),
semi-dark: mix($color, black, 75%),
dark: mix($color, black, 50%),
extra-dark: mix($color, black, 25%)
),$variant) or $color;
}
.class { color: get-color(green); }
.class { color: get-color(green, semi-light); }
.class { color: get-color(yellow, dark); }
Related
Is it possible to have a partial named class like this
.theme-color- {
&yellow {
color: yellow
}
&red {
color: red
}
&blue {
color: blue
}
}
but a generic way like this
$yellow = yellow;
$red = red;
$blue = blue;
.theme-color-#{$my-color} {
color: #{$my-color};
}
<div class="theme-color-red"></div>
You can use #each:
$colors: red, yellow, blue;
#each $color in $colors {
.theme-color-#{$color} {
color: $color;
}
}
This generates the following CSS:
.theme-color-red {
color: red;
}
.theme-color-yellow {
color: yellow;
}
.theme-color-blue {
color: blue;
}
You can also use #each with a map instead of a list if you want to specify custom color values:
$colors: (red: '#ff0000', yellow: '#fffd62', blue: '#0000ff');
#each $color, $hex in $colors {
.theme-color-#{$color} {
color: $hex;
}
}
Which results in the following CSS:
.theme-color-red {
color: "#ff0000";
}
.theme-color-yellow {
color: "#fffd62";
}
.theme-color-blue {
color: "#0000ff";
}
I have written this #mixin to set styles for light and dark theme in one line.
#mixin setThemeDynamicCSSRule($property, $light-theme-value, $dark-theme-value) {
#{$property}: #{$light-theme-value};
.dark-mode & {
#{$property}: #{$dark-theme-value}
}
}
body {
#include setThemeDynamicCSSRule(color, black, white);
#include setThemeDynamicCSSRule(background-color, white, black);
#include setThemeDynamicCSSRule(font-size, 16px, 32px);
}
This returns me:
body {
color: black;
background-color: white;
font-size: 16px;
}
.dark-mode body {
color: white;
}
.dark-mode body {
background-color: black;
}
.dark-mode body {
font-size: 32px;
}
it keeps repeating .dark-mode body selector for each individual setThemeDynamicCSSRule() call.
It still does the job but ideally I would like css to compile to:
body {
color: black;
background-color: white;
font-size: 16px;
}
.dark-mode body {
color: white;
background-color: black;
font-size: 32px;
}
Not exactly the way your are looking for ... but thinking forward it is the wanted result. And mayby an easier and more neat way to organize your special theme properties in one place.
Wrap your different theme property/values at to a nested map. Than use an adapted mixin.
$themes: (
color: (
'light': 'black',
'dark': 'white',
),
background-color: (
'light': 'white',
'dark': 'black',
),
font-size: (
'light': 16px,
'dark': 32px,
),
);
#mixin setThemeStyles($themes){
#each $property, $values in $themes {
#{$property}: map-get($values, 'light' );
}
.dark-mode & {
#each $property, $values in $themes{
#{$property}: map-get($values, 'dark');
}
}
}
body {
#include setThemeStyles($themes);
}
//######>> RESULT IS YOUR WANTED CSS
body {
color: "black";
background-color: "white";
font-size: 16px;
}
.dark-mode body {
color: "white";
background-color: "black";
font-size: 32px;
}
I have created my application toolbar with application.e4xmi.
And thereafter I tweaked my .css as below
/* JavaFX CSS - Main CSS-File */
.root {
-fx-base: rgb(222, 234, 247);
-fx-background: rgb(222, 234, 247);
}
.tab {
-fx-background-color: linear-gradient(to top, -fx-base, derive(-fx-base,30%));
}
.menu-bar {
-fx-background-color: linear-gradient(to bottom, -fx-base, derive(-fx-base,30%));
}
.tool-bar > .container{
}
.tool-bar:vertical {
}
.tool-bar:horizontal {
-fx-background: #ececec;
-fx-base: #ececec;
}
.tree-view{
-fx-focus-color: #f21a3f;
}
/*.table-view {
-fx-control-inner-background: rgb(222, 234, 247);
-fx-table-cell-border-color:derive(-fx-base,+10%);
-fx-table-header-border-color:derive(-fx-base,+20%);
} */
.split-pane:horizontal > * > .split-pane-divider {
-fx-control-inner-background : rgb(255, 255, 255);
-fx-border-color: transparent -fx-base transparent -fx-base;
-fx-background-color: transparent, derive(-fx-base,20%);
-fx-background-insets: 0, 0 1 0 1;
}
.my-gridpane {
-fx-background-color: radial-gradient(radius 100%, derive(-fx-base,20%), derive(-fx-base,-20%));
}
.separator-label {
-fx-text-fill: orange;
}
Now I am trying to change background color of high-lighted area of toolbar in my eclipse RCP application.
Any suggestion would be appreciated.
You can use a define a CSS and add this CSS to your product:
.MTrimBar {
background-color: #000000;
}
I just want to ask if it is possible in LESSCSS to use the extract function while the value is stored in LESS Variable?
This is what I'm using:
#basic: #ffffff, #000000, #333333;
#classes: white, black, gray;
.make-color(#i: length(#basic)) when (#i > 0) {
.make-color(#i - 1);
#color: extract(#basic, #i);
#class: extract(#classes, #i);
.#{class} {
color: #color;
}
}
}
This is what I want
#white: #ffffff;
#black: #000000;
#gray: #333333;
#basic: #white, #black, #gray;
#classes: white, black, gray;
.make-color(#i: length(#basic)) when (#i > 0) {
.make-color(#i - 1);
#color: extract(#basic, #i);
#class: extract(#classes, #i);
.#{class} {
color: #color;
}
}
}
When I tried the second one it returns a compiled css like this:
.white{
color: #white;
}
.black{
color: #black;
}
.gray{
color: #gray;
}
but my target is like this:
.white{
color: #ffffff;
}
.black{
color: #000000;
}
.gray{
color: #333333;
}
I have this .less file for buttons:
button {
background: #button-background;
color: #text;
&.primary {
background-image: linear-gradient(#79d858, #569e3d);
border-color: #4a993e;
color: white;
}
&.primary:hover {
background-image: linear-gradient(#89e868, #66ae4d);
border-color: #4a993e;
color: white;
}
&.primary:active,
&.primary:focus {
background-image: linear-gradient(#99f878, #76be5d);
border-color: #4a993e;
color: white;
}
}
Can someone give me advice on how I can combine all the &.primary into one ?
Is this what you want:
button {
background: #button-background;
color: #text;
&.primary {
background-image: linear-gradient(#79d858, #569e3d);
border-color: #4a993e;
color: white;
&:hover {
background-image: linear-gradient(#89e868, #66ae4d);
}
&:active, &:focus {
background-image: linear-gradient(#99f878, #76be5d);
}
}
}
DEMO