Skip to content

Commit

Permalink
fix(color-generator): contrast color calculation (#3274)
Browse files Browse the repository at this point in the history
* fix(color-generator): contrast color calculation

* refactor: apply types and code style
  • Loading branch information
sean-perkins authored Nov 30, 2023
1 parent c6807ac commit 032d0c8
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/components/page/theming/_utils/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,28 @@ const rgbToYIQ = ({ r, g, b }: RGB): number => {
return (r * 299 + g * 587 + b * 114) / 1000;
};

const RED = 0.2126;
const GREEN = 0.7152;
const BLUE = 0.0722;
const GAMMA = 2.4;

const luminance = ({ r, g, b }: RGB) => {
const a = [r, g, b].map((v) => {
v /= 255;
return v <= 0.03928 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, GAMMA);
});
return a[0] * RED + a[1] * GREEN + a[2] * BLUE;
};

// Original source: https://stackoverflow.com/a/9733420
const contrast = (rgb1: RGB, rgb2: RGB) => {
const lum1 = luminance(rgb1);
const lum2 = luminance(rgb2);
const brightest = Math.max(lum1, lum2);
const darkest = Math.min(lum1, lum2);
return (brightest + 0.05) / (darkest + 0.05);
};

export class Color {
readonly hex: string;
readonly hsl: HSL;
Expand Down Expand Up @@ -176,8 +198,11 @@ export class Color {
return /(^#[0-9a-fA-F]+)/.test(value.trim());
}

contrast(threshold = 128): Color {
return new Color(this.yiq >= threshold ? '#000' : '#fff');
contrast(): Color {
const blackContrastRatio = contrast(this.rgb, { r: 0, g: 0, b: 0 });
const whiteContrastRatio = contrast(this.rgb, { r: 255, g: 255, b: 255 });

return new Color(blackContrastRatio >= whiteContrastRatio ? '#000' : '#fff');
}

mix(from: string | RGB | HSL | Color, amount = 0.5): Color {
Expand Down

1 comment on commit 032d0c8

@vercel
Copy link

@vercel vercel bot commented on 032d0c8 Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ionic-docs – ./

ionic-docs-gqykycf8t.vercel.app
ionic-docs-ionic1.vercel.app
ionic-docs-git-main-ionic1.vercel.app

Please sign in to comment.