Skip to content

Commit

Permalink
fix: wrong hex color values (#2985)
Browse files Browse the repository at this point in the history
Closes #2983

## Changes:

- fix `Color.toHex()` and `Color._componentToHex()` to handle fractional numbers, negative numbers and colors with transparency
- add tests for `Color.toHex()`
  • Loading branch information
mcavazotti authored Mar 30, 2024
1 parent 9cdea7c commit 737f87c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ will be positioned with the top left of the graphic at the actor's position.
- Fixed issue where Firefox on Linux would throw an error when using custom Materials due to unused attributes caused by glsl compiler optimization.
- Fixed issue where start transition did not work properly if deferred
- Fixed issue where transitions did not cover the whole screen if camera was zoomed
- Fixed issue where `Color.toHex()` produced invalid strings if the channel values are negative or fractional, or if the alpha channel was different than 1

### Updates

Expand Down
11 changes: 8 additions & 3 deletions src/engine/Color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,15 +222,20 @@ export class Color {
* @see https://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb
*/
private _componentToHex(c: number) {
const hex = c.toString(16);
// Handle negative and fractional numbers
const hex = Math.max(Math.round(c), 0).toString(16);
return hex.length === 1 ? '0' + hex : hex;
}

/**
* Return Hex representation of a color.
*/
public toHex() {
return '#' + this._componentToHex(this.r) + this._componentToHex(this.g) + this._componentToHex(this.b);
let hexRepresentation = '#' + this._componentToHex(this.r) + this._componentToHex(this.g) + this._componentToHex(this.b);
if (this.a !== 1) {
hexRepresentation += this._componentToHex(this.a * 255);
}
return hexRepresentation;
}

/**
Expand Down Expand Up @@ -413,7 +418,7 @@ export class Color {
* http://axonflux.com/handy-rgb-to-hsl-and-rgb-to-hsv-color-model-c
*/
class HSLColor {
constructor(public h: number, public s: number, public l: number, public a: number) {}
constructor(public h: number, public s: number, public l: number, public a: number) { }

public static hue2rgb(p: number, q: number, t: number): number {
if (t < 0) {
Expand Down
11 changes: 11 additions & 0 deletions src/spec/ColorSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ describe('A color', () => {
expect(color.a).toBe(31 / 255);
});

it('generate valid hex representation', () => {
color = ex.Color.White;
expect(color.toHex()).toBe('#ffffff');
color = ex.Color.Green;
expect(color.toHex()).toBe('#00ff00');
color = ex.Color.fromRGB(17,17,17,0.1);
expect(color.toHex()).toBe('#1111111a');
color = ex.Color.fromRGB(16.9, 16.9, 16.9);
expect(color.toHex()).toBe('#111111');
});

it('can be darkened', () => {
color = ex.Color.White.clone();
color = color.darken();
Expand Down

0 comments on commit 737f87c

Please sign in to comment.