Skip to content

mix() returns wrong result for added colors. #1673

Closed

Description

@dtinth

When sending a color created from adding two colors together to the mix() function,

This is reproducable in LESS 1.4.1 and 1.5.1.

Test case 1

a {
  color: mix(#008 + #800, #FFF, 50%);
}

Expected result:

color: #c480c4;

Actual result:

color: #880088;

Test case 2

@a: #222;      // #222
@b: @a + #224; // #446
@c: #888;      // #888

a {
  a: @a;
  b: @b;
  c: @c;
  d: mix(@b, @c, 50%); // should be #667
}

Expected result:

d: #666677

Actual result:

d: #444455

Some more tests:

// These example should all equal #999

mix(#333, #fff, 50%) // #999999
mix(#222 + #111, #fff, 50%) // #333333
mix(#333, #eee + #111, 50%) // #FFFFFF
mix(#222 + #111, #eee + #111, 50%) // #999999
mix(#333 + #000, #fff + #000, 50%) // #999999

Activity

  1. seven-phases-max commented on Nov 22, 2013

    @seven-phases-max
    Member

    Interesting. I found that the problem is in alpha channel. Basically, an addition (or any other op) of two colors with alpha = 1 results in a color with alpha = 2, thus unexpected result when the result color passed to certain functions.

  2. lukeapage commented on Nov 22, 2013

    @lukeapage
    Member

    Great spot.

  3. lukeapage commented on Dec 22, 2013

    @lukeapage
    Member

    @seven-phases-max assume this is now fixed?

  4. seven-phases-max commented on Dec 23, 2013

    @seven-phases-max
    Member

    Yes, it is fixed.

  5. tomek-he-him commented on Feb 2, 2015

    @tomek-he-him

    @seven-phases-max, I'm expecting mix(@a, @b, 25%) to return the same result as overlaying @a with 1/4th of its opacity over an original-opacity-@b in any image editor. I guess this goes inline with the discussion in #2291. Do I get this right?

    Now, the spec coming from our designer says that #2274AC + 25% black should equal #195781. I've double-checked in Inkcape and GIMP, getting exactly the same results:

    mix

    And even so, less.js v2.3.1 returns #1a5781:

    $ echo "a { b: mix(black, #2274AC, 25%); }" | lessc -
    a {
      b: #1a5781;
    }

    I know it's only one unit in one channel – but enough to be another color than that from the spec. It does make a difference in our case.

  6. seven-phases-max commented on Feb 2, 2015

    @seven-phases-max
    Member

    @tomekwi

    Let's see:
    mix(black, #2274AC, 25%); ->
    25% * black + 75% * #2274AC ->
    75% * [34 167 129] -> [25.5 87 129] and
    round(25.5) -> 26 -> 0x1a.

    I.e. Less uses canonical rounding for non-integer color channel values when producing their hexadecimal representation while those graphics editors you mentioned seem to use floor for some reason. That's it. Note the rounding is not even a part of the mix function but the property of any rgb to hex conversion, e.g.:

    div {
        color: (rgb(34, 116, 172) * 0.75); // -> #1a5781
    }
    

    So I can't see any issue here or anything that should or may be fixed at all (at Less side ;)

  7. tomek-he-him commented on Feb 2, 2015

    @tomek-he-him

    @seven-phases-max, thanks for clearing this out – could've checked myself actually :) To be honest, I only noticed that the difference is so small at the very end of writing the comment.

    I'm almost certain our designer has used Photoshop for the conversion. So maybe it's some kind of standard amond image editors? If it is so, would you consider adjusting LESS's color rounding algorhithm?

    I admit it doesn't make sense to floor the values. But you do aim to mimic the behaviour of image editors, don't you?

  8. lukeapage commented on Feb 2, 2015

    @lukeapage
    Member

    Is there a reason gimp uses different rounding though? If every other tool
    has a different rounding method, shouldnt we change?

  9. lukeapage commented on Feb 2, 2015

    @lukeapage
    Member

    Reported before. Seems sass also floors.
    #552

  10. lukeapage commented on Feb 2, 2015

    @lukeapage
    Member

    Cant find any more info. I suspect flooring may have become the norm
    because the first implementation cast a float to an int which floors right?

  11. seven-phases-max commented on Feb 3, 2015

    @seven-phases-max
    Member

    @lukeapage

    Yeah, I wondered myself. I asked to test a few graphics products from Adobe and they actually seem to use 0.5 -> 0 rounding instead of 0.5 -> 1 (but not really floor, i.e. it's still 0.51 -> 1). I can't imagine any sensible reason they do this except some low-level algorithm/optimization compromise and/or backward-compatibility with 1960's.

  12. lukeapage commented on Feb 3, 2015

    @lukeapage
    Member

    Interesting.. 'round down' rounding. At least thats an even smaller change
    from what we do - unless its accountancy rounding (where round up/down
    flips depending on whether the number is odd/even) ?

  13. tomek-he-him commented on Feb 19, 2015

    @tomek-he-him

    Should we reopen this issue? Merge into #2291?

    Although to me #2291 seems to be a misunderstanding rather than a real problem.

  14. seven-phases-max commented on Feb 19, 2015

    @seven-phases-max
    Member

    @tomekwi

    I don't think this one should be reopened since it's about wrong result when mixing non-transparent colors and it's fixed (the fact that the error was in "alpha-compositing" code does not really matter). #2291 is about "alpha-compositing" algorithm itself and any hypothetical changes there won't affect the code example above. I kept #2291 open just in case someone comes up with ideas/rationale to use some another algo (but I guess it's most likely to be closed as time passes "due to inactivity" or so).

  15. tomek-he-him commented on Feb 19, 2015

    @tomek-he-him

    I can make another issue out of this, if you consider changing the current rounding.

  16. seven-phases-max commented on Feb 19, 2015

    @seven-phases-max
    Member

    Please do if you feel this is important. Honestly I don't think this will be ever considered too seriously... Changing standard JavaScript round function there to some hand-written round-down method does not look like something that's worth 1/2^24 drift in a color value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions