Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix maths for lib/functions/adjust()#2451

Open
buzuck wants to merge 1 commit intostylus:devfrom
buzuck:fix-adjust-lightness-maths
Open

Fix maths for lib/functions/adjust()#2451
buzuck wants to merge 1 commit intostylus:devfrom
buzuck:fix-adjust-lightness-maths

Conversation

@buzuck
Copy link

@buzuck buzuck commented Mar 6, 2019

The calculation of the returned value is awkward in case of a percentage of lightness.
However, as this error is here since quite a long time, fixing will surely break stuff...

Sorry about the double PR... :/


Take hsl.l = 60. Increasing it's value of 50% should give as a result 90%, and 30% when decreasing it.
The decreasing part is OK, however increasing the lightness by 50% gives 80%.

The current code, when used with percentages and lightness is equivalent to:

if ( val > 0 ) {
    val = (100 - hsl[prop]) * val / 100
} else {
    val = hsl[prop] * (val / 100);
}
hsl[prop] += val;

Numerically, with values as given above (val = 50, hsl.l = 60):

if ( val > 0 ) {
    val = (100 - 60) * 50 / 100 = 20;
} else {
    val = 60 * (50 / 100) = 30; /* Or = -30 ; as if we are here, val < 0 */
}

Thus, if we want to have 90% as a result, only the else part is valid.

@buzuck
Copy link
Author

buzuck commented Mar 7, 2019

Mh... It seems this is definitely a breaking change, considering the tests failures.

Here is a dirty fix:

lightness-fix(color, percentage)
	lightness(color, lightness(color) + lightness(color) * percentage / 100)

a = hsl(0, 0%, 60%)

b = a + 50%              /* Legacy behavior: b = hsl(0, 0%, 80%) */
b = lightness-fix(a, 50) /* New behavior: b = hsl(0, 0%, 90%) */

@iChenLei
Copy link
Member

Any update ? Bro

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments