Finding “equivalent” color with opacity

后端 未结 6 970
深忆病人
深忆病人 2020-12-12 09:56

Say I have a background color with a \"ribbon\" running over it in another solid color. Now, I want the ribbon to be partially transparent to let some details blend through,

6条回答
  •  失恋的感觉
    2020-12-12 10:11

    i made a LESS mixin using Phrogz' answer. you input:

    1. how the colour should look
    2. with a certain alpha
    3. on a given background (default being white)

    Here's the code:

    .bg_alpha_calc (@desired_colour, @desired_alpha, @background_colour: white) {
        @r3: red(@desired_colour);
        @g3: green(@desired_colour);
        @b3: blue(@desired_colour);
    
        @r2: red(@background_colour);
        @g2: green(@background_colour);
        @b2: blue(@background_colour);
    
        // r1 = (r3 - r2 + r2 * a1) / a1
        @r1: ( @r3 - @r2 + (@r2 * @desired_alpha) ) / @desired_alpha;
        @g1: ( @g3 - @g2 + (@g2 * @desired_alpha) ) / @desired_alpha;
        @b1: ( @b3 - @b2 + (@b2 * @desired_alpha) ) / @desired_alpha;
    
        background-color: @desired_colour;
        background-color: rgba(@r1, @g1, @b1, @desired_alpha);
    
    }
    

    Usage like so:

    @mycolour: #abc;
    @another_colour: blue;
    .box_overlay {
      // example:
      .bg_alpha_calc (@mycolour, 0.97, @another_colour);
      // or (for white bg) just:
      .bg_alpha_calc (@mycolour, 0.97);
    }
    

    Obviously doesn't work for impossible combinations (as mentioned by Phrogz), that means only mild levels of transparency are supported. See how you go with it.

提交回复
热议问题