Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Fixed color lightening changing hue, as well as add screen color blending mode #617

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,19 @@ export class Color {
}

lighten(a: number): Color {
return new Color(this.r + a, this.g + a, this.b + a)
return new Color(
(255 - this.r) * a + this.r,
(255 - this.g) * a + this.g,
(255 - this.b) * a + this.b
)
}

darken(a: number): Color {
return this.lighten(-a)
return new Color(
this.r * (1-a),
this.g * (1-a),
this.b * (1-a)
)
}

invert(): Color {
Expand All @@ -196,6 +204,14 @@ export class Color {
this.b * other.b / 255,
)
}

screen(other: Color): Color {
return new Color(
255 - (255-this.r) * (255-other.r) / 255,
255 - (255-this.g) * (255-other.g) / 255,
255 - (255-this.b) * (255-other.b) / 255,
)
}

eq(other: Color): boolean {
return this.r === other.r
Expand Down
35 changes: 31 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3247,16 +3247,43 @@ export declare class Color {
static WHITE: Color
static BLACK: Color
clone(): Color

/**
* Lighten the color (adds RGB by n).
* Lighten color by a certain percentage
* @param percent Number between 0-1 indicating how light to make color
* @example
* color.lighten(0); // Does nothing
* color.lighten(0.5); // 50% lighter
* color.lighten(1); // White
* @returns Lightened color
*/
lighten(n: number): Color
lighten(percent: number): Color
/**
* Darkens the color (subtracts RGB by n).
* Darken color by a certain percentage
* @param percent Number between 0-1 indicating how dark to make color
* @example
* color.darken(0); // Does nothing
* color.darken(0.5); // 50% darker
* color.darken(1); // Black
* @returns Darkened color
*/
darken(n: number): Color
darken(percent: number): Color
invert(): Color

/**
* Apply multiply blend mode on color
* @param other Color to blend with
* @returns Result of blended colors
*/
mult(other: Color): Color

/**
* Apply screen blend mode on color
* @param other Color to blend with
* @returns Result of blended colors
*/
screen(other: Color): Color

eq(c: Color): boolean
toString(): string
}
Expand Down