polished icon indicating copy to clipboard operation
polished copied to clipboard

[V2] rgba function should behave like Sass rgba function

Open tolgaergin opened this issue 9 years ago • 17 comments

The rgba function that you created expects ${rgba(255, 205, 100, 0.7)} or ${rgba({ red: 255, green: 205, blue: 100, alpha: 0.7 })}; when we use it in styled-components.

I don't know, my approach might be wrong but If I use your rgba function like ${rgba('#000', 0.1)} in order to get the same css rgba(0, 0, 0, 0.1) when I use rgba function in Sass, it would be better.

I use this function to get the result that I mentioned.

const hexToRgb = (hex) => {
  // http://stackoverflow.com/a/5624139
  var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
  hex = hex.replace(shorthandRegex, function (m, r, g, b) {
    return r + r + g + g + b + b;
  });

  var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  return result ? {
    r: parseInt(result[1], 16),
    g: parseInt(result[2], 16),
    b: parseInt(result[3], 16),
  } : null;
};

const rgba = (hex, alpha) => {
  const color = hexToRgb(hex);
  return `rgba(${color.r}, ${color.g}, ${color.b}, ${alpha})`;
};

export const Header = styled.div `
  box-shadow: 0 0 5px ${rgba('#000', 0.1)};
`;

I wonder your ideas about it.

I look forward to use Polished in a real project. Thanks

tolgaergin avatar Mar 09 '17 22:03 tolgaergin

Interesting case. The functionality is already covered by https://polished.js.org/docs/#transparentize

To be honest I'm not sure. I thought this would not be needed, but if this is a very common case for Sass users we might want to consider supporting it.

nikgraf avatar Mar 10 '17 08:03 nikgraf

Thanks for sharing your thoughts Nik,

I've been asking many people around me for 2 weeks. the most common built-in functions in sass in my circle are that 1) rgba 2) lighten, darken

Additionally, I've investigated the most popular front-end libraries use Sass, if I compare usage of rgba(#hex, alpha) or transparentize(alpha, #hex) in that libraries

Library rgba usage transparentize usage
Bootstrap >3+ 0
Bourbon >3+ 0
Foundation >3+ 0
Ionic >3+ 0
Materialize 0 1
Bulma >3+ 0

Personally, I always preferred to use rgba rather than transparentize due to these reason

  1. It is simple just 4 letters
  2. Easy to write while setting colors #34d instead of rgba(51, 68, 221, 1)
  3. Easy to remember hex codes
  4. When collecting colors on Sketch or Photoshop, easy to copy hex code and give the alpha channel
  5. It is the same experience on Sketch or Photoshop like opacity. with Transparentize, you need to subtract (1 - opacity) to get the same result that you see on design applications.

tolgaergin avatar Mar 10 '17 16:03 tolgaergin

I do see where this would be useful, what we would need to sort out though is how this jives with what RGBA currently does for us. Would the API become a bit too confusing if it supported both this use case as well as what it currently does?

bhough avatar Mar 13 '17 15:03 bhough

@bhough shouldn't be too crazy to do since we can match for the amount of arguments

I'm still be undecided if we want to encourage a simpler API with the explicit function or opt in for this special case …

nikgraf avatar Mar 21 '17 03:03 nikgraf

@tolgaergin thanks a ton for the research! 🙌

nikgraf avatar Mar 21 '17 03:03 nikgraf

@nikgraf agreed, meant more confusing as it seems to be doing two different things using the same method. If that pattern is already established, it may not be too bad to just implement it as opt in, while leaving transparentize in place.

bhough avatar Mar 21 '17 14:03 bhough

question is: do we want this now or wait a bit longer until more people ask for it?

nikgraf avatar Mar 21 '17 15:03 nikgraf

In the interest of getting a release out soon(ish), I would vote for us to wait. If we lacked a method for doing this it would be different, but with tranparentize, at worst it's a minor inconvenience.

bhough avatar Mar 21 '17 15:03 bhough

sounds good!

nikgraf avatar Mar 21 '17 16:03 nikgraf

@bhough Max was thinking about launching at React London. I tried to reach you via private message on Twitter.

nikgraf avatar Mar 21 '17 16:03 nikgraf

Personally I always use rgba like this; rgba(#fff, .5). I think this should be supported.

ghost avatar Apr 06 '17 23:04 ghost

I hope the polished can support multiple function like ${rgba('#000', 0.1)} or ${rgba('#cbcbcb', 0.1)} or ${rgba(200, 200, 200, 0.1)} or ${rgba(green, 0.1)}. Thank you!

hylurk avatar May 03 '17 08:05 hylurk

@nikgraf @mxstbr I think at this point there is enough demand to warrant implementing this. Any objections?

bhough avatar May 04 '17 22:05 bhough

Just left a comment on the PR. We are going to merge this asap.

nikgraf avatar Jun 21 '17 08:06 nikgraf

@nikgraf , @bhough Just wanted to say thanks for getting this feature added in! You guys do some great work.  😃👏

alocke12992 avatar Jan 08 '19 19:01 alocke12992

didn't mention me @alocke12992 😭 😄

tolgaergin avatar Jan 08 '19 19:01 tolgaergin

Found another issue with rgba and gradient functions. This cause an error: background-image: linear-gradient(to right, ${rgba('#DE6262', .2)}, ${rgba('#FFB88C', .2)});

Use this instead export const linearGradient = string => { return linear-gradient(${string}) }

Usage example: background-image: ${linearGradient(to right, ${rgba('#DE6262', .2)}, ${rgba('#FFB88C', .2)})};

I hope someone will find this solution usefull.

AdamGulmantowicz avatar Mar 24 '20 20:03 AdamGulmantowicz