Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toRgb and similar functions should take into account already converted colors #21

Open
Tokimon opened this issue May 18, 2020 · 1 comment

Comments

@Tokimon
Copy link

Tokimon commented May 18, 2020

The functions toRgb, toHsl etc. are some practical functions, but only if the given color is a string. It could be very nice if they could take into account colors that have already been converted by fx. hexToRgb like:

toHsl(hexToRgb('#ffdd56'))

The above code will fail because the argument passed into toHsl is no longer a string. This is useful when you make a utility function that takes in some sort of color and you just want to make sure that it is a rgb object for manipulating.

concretely I have some code like this:

// My utility function
export const mix = (base, mixed, mixPct) => {
  mixPct = pct(mixPct);

  if (mixPct === 1) { return isString(mixed) ? mixed : formatRgb(mixed); }
  if (mixPct === 0) { return isString(base) ? base : formatRgb(base); }

  if (isString(base)) { base = toRgb(base); }
  if (isString(mixed)) { mixed = toRgb(mixed); }

  const clr = colorMix(base, mixed, mixPct);
  return formatRgb(clr);
};

It could be nice not to have to do:

if (isString(base)) { base = toRgb(base); }
if (isString(mixed)) { mixed = toRgb(mixed); }

just because I want to make sure the color is in the RGB format.

In my case I am testing it something like this:

  it('Should mix "rgba" colors', () => {
      const baseRgba = toRgb('#f3bc56');
      baseRgba.alpha = 0.4;

      const mixedRgba = toRgb('#53fcd6');
      mixedRgba.alpha = 0.6;

      expect(mix(baseRgba, mixedRgba, 40)).toBe('rgba(179,213,137,0.48)');
    });
@Tokimon
Copy link
Author

Tokimon commented May 18, 2020

On a side note to this, it could be cool if the mix method could do the conversion under the hood so I don't have use toRgb (or similar) before to perform the mixture.

So this:

mix('#f0b', '#b1f', 0.5);

Instead_ of this:

mix(hexToRgb('#f0b'),hexToRgb( '#b1f'), 0.5);

@Tokimon Tokimon changed the title toRgb and similar function should take into account already converted colors toRgb and similar functions should take into account already converted colors May 18, 2020
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

No branches or pull requests

1 participant