Home / Class/ Color Class — react Architecture

Color Class — react Architecture

Architecture documentation for the Color class in colors.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  78702cf1_14a2_6e10_63a6_da3c6a4e7f89["Color"]
  53f7916e_4fad_5786_8457_cb734d2e7688["colors.ts"]
  78702cf1_14a2_6e10_63a6_da3c6a4e7f89 -->|defined in| 53f7916e_4fad_5786_8457_cb734d2e7688
  b5132618_4c10_4810_55de_61f7ab4aee7b["constructor()"]
  78702cf1_14a2_6e10_63a6_da3c6a4e7f89 -->|method| b5132618_4c10_4810_55de_61f7ab4aee7b
  1434c607_9ae9_3943_a519_f032bd5dd194["toAlphaString()"]
  78702cf1_14a2_6e10_63a6_da3c6a4e7f89 -->|method| 1434c607_9ae9_3943_a519_f032bd5dd194
  7ea6f7fc_4d9d_8347_f642_c953470c826e["toString()"]
  78702cf1_14a2_6e10_63a6_da3c6a4e7f89 -->|method| 7ea6f7fc_4d9d_8347_f642_c953470c826e
  0b664c6e_2a3b_0e74_0ea1_e8c10bb06f14["adjusted()"]
  78702cf1_14a2_6e10_63a6_da3c6a4e7f89 -->|method| 0b664c6e_2a3b_0e74_0ea1_e8c10bb06f14
  ffefc51a_8d5c_dbcd_64c7_beed69f03080["toCssString()"]
  78702cf1_14a2_6e10_63a6_da3c6a4e7f89 -->|method| ffefc51a_8d5c_dbcd_64c7_beed69f03080
  042e3558_4fd3_8d7f_f4ad_f1ffa05ba3cd["redistribute()"]
  78702cf1_14a2_6e10_63a6_da3c6a4e7f89 -->|method| 042e3558_4fd3_8d7f_f4ad_f1ffa05ba3cd

Relationship Graph

Source Code

compiler/packages/react-forgive/client/src/colors.ts lines 12–60

export class Color {
  constructor(
    private r: number,
    private g: number,
    private b: number,
  ) {}

  toAlphaString(a: number) {
    return this.toCssString(a);
  }
  toString() {
    return this.toCssString(1);
  }

  /**
   * Adjust the color by a multiplier to lighten (`> 1.0`) or darken (`< 1.0`) the color. Returns a new
   * instance.
   */
  adjusted(mult: number) {
    const adjusted = Color.redistribute([
      this.r * mult,
      this.g * mult,
      this.b * mult,
    ]);
    return new Color(...adjusted);
  }

  private toCssString(a: number) {
    return `rgba(${this.r},${this.g},${this.b},${a})`;
  }
  /**
   * Redistributes rgb, maintaing hue until its clamped.
   * https://stackoverflow.com/a/141943
   */
  private static redistribute([r, g, b]: RGB): RGB {
    const threshold = 255.999;
    const max = Math.max(r, g, b);
    if (max <= threshold) {
      return [int(r), int(g), int(b)];
    }
    const total = r + g + b;
    if (total >= 3 * threshold) {
      return [int(threshold), int(threshold), int(threshold)];
    }
    const x = (3 * threshold - total) / (3 * max - total);
    const gray = threshold - x * max;
    return [int(gray + x * r), int(gray + x * g), int(gray + x * b)];
  }
}

Frequently Asked Questions

What is the Color class?
Color is a class in the react codebase, defined in compiler/packages/react-forgive/client/src/colors.ts.
Where is Color defined?
Color is defined in compiler/packages/react-forgive/client/src/colors.ts at line 12.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free