styled-components / polished

A lightweight toolset for writing styles in JavaScript ✨
https://polished.js.org/
MIT License
7.6k stars 209 forks source link

readableColor not returning the correct accessible value #555

Closed dgattoni closed 3 years ago

dgattoni commented 3 years ago

Mixin/Helper/Shorthand Usage

readableColor("purple",  "pink",  "navy", true);

What You Are Seeing

The Problem

on readableColor unit tests, the it block on line 13 "should return custom light background when passed dark color" is being tested passing a dark colour as the 2nd argument and light color as the 3rd argument, however, the function input signature expects a light colour as the 2nd param and dark colour as the 3rd parameter.

What You Expected To See

As specified on the documentation:

Returns black or white (or optional light and dark return colors) for best contrast depending on the luminosity of the given color.

So, following the current function input signature, if I pass the following values:

Screen Shot 2020-11-24 at 3 41 35 pm
// purple as my given color
// pink as my optional light color, 2nd arg
// navy as my optional dark color, 3rd arg

 readableColor("purple",  "pink", "navy", true);

I expect the returning value to be my optional light color "pink", as it has no contrast issues with "purple", (the "given color") but instead, I got "white" as the defaultLightReturnColor.

Reproduction

I have created a CodeSandbox example to reproduce the issue

bhough commented 3 years ago

@dgattoni This is working as intended, albeit with slightly confusing parameter names. lightReturnColor is the color you want to be returned for light backgrounds and vice versa for dark. We can update to be more explicit (lightComparisonReturnColor possibly). The way to reason about it is we can't guarantee you are actually passing a light color so we are naming it after the comparison color vs the color you are passing. You are seeing white correctly because of strict mode kicking it when you place pink on a light background in your example.

bhough commented 3 years ago

Docs and signature updated in v4.0.5.

dgattoni commented 3 years ago

Awesome! Thank you @bhough for the update! 🙌