2

I realise this is a similar question to this Conditional CSS based on background color variable

but I need to do it inside a loop in LESS. If a background colour is too dark I want to generate another class so I can make the text on top lighter but not sure how as I don't think you can use lighten and darken functions with hex colours...?

Here is my LESS http://codepen.io/anon/pen/IlsJE?editors=110

.for(@i, @n) {.-each(@i)}
.for(@n)     when (isnumber(@n)) {.for(1, @n)}
.for(@i, @n) when not (@i = @n)  {
.for((@i + (@n - @i) / abs(@n - @i)), @n);}

// .for-each

.for(@array)   when (default()) {.for-impl_(length(@array))}
.for-impl_(@i) when (@i > 1)    {.for-impl_((@i - 1))}
.for-impl_(@i)                  {.-each(extract(@array, @i))}

// PAs
@pa1: "pa1";
@pa2: "pa2";
@pa3: "pa3";
@pa4: "pa4";

// Colors
@pa1-color: #72afb6;
@pa2-color: #9fad9f;
@pa3-color: #8dd8f8;
@pa4-color: #00567A;

// Setting variables and escaping them
@pas: ~"pa1" ~"pa2" ~"pa3" ~"pa4";

// Define our variable   
.define(@var) {
  @pa-color-primary: '@{var}-color';
}

// Starting the PA mixin
.pacolors() {
  // Generating the loop for each PA
  .for(@pas); .-each(@name) {
    // After loop happens, it checks what brand is being called
    .define(@name);
    .@{name} .bg-accent {
        background-color: @@pa-color-primary;
    }
  }
}

.pacolors();

Any help would be appreciated.

5
  • 2
    Is this what you need? You just need to use the contrast function within the loop. Commented Jul 23, 2014 at 13:05
  • 2
    And just in case, it's too much unnecessary code in your snippet. Here's simplified codepen (forked from the one kindly provided by Harry). Commented Jul 23, 2014 at 16:05
  • Good stuff. Thanks guys. Not sure how to make that an Answer, but if you can, go ahead! Commented Jul 23, 2014 at 16:09
  • @seven-phases-max: Do you want to go ahead with the answer mate? I think you have put in more effort on this one by simplifying it than I did in adding that line :) Xav - Nice to know it helped. Commented Jul 23, 2014 at 16:14
  • 1
    @Harry, nope (I'm running away right now) - please feel free to answer. Commented Jul 23, 2014 at 16:17

1 Answer 1

2

You can achieve this by using the built-in contrast function provided by LESS.

// Starting the PA mixin
.pacolors() {
  // Generating the loop for each PA
  .for(@pas); .-each(@name) {
    // After loop happens, it checks what brand is being called
    .define(@name);
    .@{name} .bg-accent {
        background-color: @@pa-color-primary;
          color: contrast(@@pa-color-primary,
                          lighten(@@pa-color-primary, 100%),
                          darken(@@pa-color-primary, 100%),10%);  
          /* syntax - contrast(color-for-comparison,
                               color1 - lighten (100%) is essentially white,
                               color 2 - darken (100%) is essentially black,
                               threshold percentage based on which decision is taken
           */
    }
  }
}

Demo | LESS Function Spec - Contrast

Simplified Version: (Courtesy - seven-phases-max)

// Colors
@pas:
    pa1 #72afb6,
    pa2 #9fad9f,
    pa3 #8dd8f8,
    pa4 #00567A;

// Styles
& {
    .for(@pas); .-each(@pa) {
         @name:  extract(@pa, 1);
         @color: extract(@pa, 2);
        .@{name} .bg-accent {
            background-color: @color;
              color: contrast(@color, white, black, 10%);        
        }
    }
}

p {padding: 10px}

// ........................................................

@import "https://raw.githubusercontent.com/seven-phases-max/less.curious/master/src/for";

Demo 2

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.