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

Color helpers #149

Closed
petehunt opened this issue Jul 4, 2020 · 1 comment
Closed

Color helpers #149

petehunt opened this issue Jul 4, 2020 · 1 comment

Comments

@petehunt
Copy link
Collaborator

petehunt commented Jul 4, 2020

Made a little color helper, it's pretty useful when you have a color palette

class ColorObj extends String {
  private readonly r: number;
  private readonly g: number;
  private readonly b: number;
  private readonly a: number;

  constructor(r: number, g: number, b: number, a: number = 1) {
    if (a === 1) {
      super(`rgb(${r}, ${g}, ${b})`);
    } else {
      super(`rgba(${r}, ${g}, ${b}, ${a})`);
    }

    this.r = r;
    this.g = g;
    this.b = b;
    this.a = a;
  }

  alpha(a: number): Color {
    return new ColorObj(this.r, this.g, this.b, this.a * a) as Color;
  }
}

export type Color = ColorObj & string;

export function rgba(r: number, g: number, b: number, a: number = 1): Color {
  return new ColorObj(r, g, b, a) as Color;
}
@meyer
Copy link
Collaborator

meyer commented Dec 13, 2020

There are a number of JS color libraries out there that provide functionality ranging from color creation all the way to advanced color manipulation (blending modes, darken/lighten, interpolating between colors, etc.). Unless we’re doing something jsxstyle-specific with color helpers, I’d rather point folks to an existing color library rather than start maintaining one. Any color object that stringifies to a valid CSS color is compatible with jsxstyle (might be a good idea to point that out in the README).

I’m going to close this for now, but if there’s something jsxstyle-specific you were thinking of doing, feel free to reopen. I’m very much into the idea of providing useful utilities in jsxstyle.

@meyer meyer closed this as completed Dec 13, 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

2 participants