Skip to content

Commit

Permalink
fix: Colorus.invert() cannot invert colors correctly (#11)
Browse files Browse the repository at this point in the history
- Now Colorus.invert() uses this.rgb for a RGB representation instead of  this.#data.rgb
- Add unit tests for invert() function in compose.spec.js module
- Add unit tests for Colorus.invert() method in main.spec.js module
  • Loading branch information
supitsdu authored Jun 10, 2024
1 parent 3e5b6a2 commit 0ff90bc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
4 changes: 1 addition & 3 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export class Colorus {
* @return {Colorus} A new Colorus instance representing the color with inverted color values.
*/
invert() {
return new Colorus(compose.invert(this.#data.rgb))
return new Colorus(compose.invert(this.rgb))
}

/**
Expand All @@ -206,5 +206,3 @@ export class Colorus {
return new Colorus(compose.rgbToGray(this.rgb, useNTSCFormula))
}
}

// console.log(new Colorus('aliceblue').rgb)
20 changes: 19 additions & 1 deletion test/compose.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { modBy, mix, lighten, saturate, hue, alpha, rgbToGray } from '../src/compose'
import { modBy, mix, lighten, saturate, hue, alpha, rgbToGray, invert } from '../src/compose'

describe('modBy', () => {
test('should correctly modify a value', () => {
Expand Down Expand Up @@ -68,6 +68,24 @@ describe('saturate', () => {
})
})

describe('invert', () => {
test('should invert black to white correctly', () => {
const color = { r: 255, g: 255, b: 255 }
const result = invert(color)
expect(result.r).toBe(0)
expect(result.g).toBe(0)
expect(result.b).toBe(0)
})

test('should invert white to black correctly', () => {
const color = { r: 0, g: 0, b: 0 }
const result = invert(color)
expect(result.r).toBe(255)
expect(result.g).toBe(255)
expect(result.b).toBe(255)
})
})

describe('hue', () => {
test('should adjust hue of an HSL color correctly', () => {
const color = { h: 180, s: 50, l: 50 }
Expand Down
6 changes: 6 additions & 0 deletions test/main.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,9 @@ describe('Colorus.test()', () => {
expect(Colorus.test(undefined)).toBeNull()
})
})

describe('Colorus.invert()', () => {
test('should invert colors correctly', () => {
expect(new Colorus('crimson').invert().toRgb()).toBe('rgb(35, 235, 195)')
})
})

0 comments on commit 0ff90bc

Please sign in to comment.