mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 00:01:37 +08:00

* chore: Switch to Node 20 + Vitest * chore: migrate to vitest mock functions * chore: code style (switch to prettier) * test: re-enable long-running test Seems the switch to Node 20 and Vitest has vastly improved the code's and / or the test's runtime! see #1193 * chore: code style * chore: fix failing tests * Updated Documentation in README.md * Update contribution guidelines to state usage of Prettier * fix: set prettier printWidth back to 80 * chore: apply updated code style automatically * fix: set prettier line endings to lf again * chore: apply updated code style automatically --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
50 lines
1.9 KiB
JavaScript
50 lines
1.9 KiB
JavaScript
import { approximatelyEqualHsv, hsvToRgb, rgbToHsv } from '../RgbHsvConversion'
|
|
|
|
describe('hsvToRgb', () => {
|
|
// Expected RGB-values taken from https://www.rapidtables.com/convert/color/hsv-to-rgb.html
|
|
it('should calculate the correct RGB values', () => {
|
|
expect(hsvToRgb(0, 0, 0)).toEqual([0, 0, 0])
|
|
expect(hsvToRgb(0, 0, 1)).toEqual([255, 255, 255])
|
|
expect(hsvToRgb(0, 1, 1)).toEqual([255, 0, 0])
|
|
expect(hsvToRgb(60, 1, 1)).toEqual([255, 255, 0])
|
|
expect(hsvToRgb(120, 1, 1)).toEqual([0, 255, 0])
|
|
expect(hsvToRgb(240, 1, 1)).toEqual([0, 0, 255])
|
|
expect(hsvToRgb(300, 1, 1)).toEqual([255, 0, 255])
|
|
expect(hsvToRgb(180, 0.5, 0.5)).toEqual([64, 128, 128])
|
|
expect(hsvToRgb(234, 0.14, 0.88)).toEqual([193, 196, 224])
|
|
expect(hsvToRgb(330, 0.75, 0.5)).toEqual([128, 32, 80])
|
|
})
|
|
})
|
|
|
|
describe('rgbToHsv', () => {
|
|
// "approximatelyEqualHsv" needed because of small deviations due to rounding for the RGB-values
|
|
it('should calculate the correct HSV values', () => {
|
|
expect(approximatelyEqualHsv(rgbToHsv(0, 0, 0), [0, 0, 0])).toEqual(true)
|
|
expect(approximatelyEqualHsv(rgbToHsv(255, 255, 255), [0, 0, 1])).toEqual(
|
|
true
|
|
)
|
|
expect(approximatelyEqualHsv(rgbToHsv(255, 0, 0), [0, 1, 1])).toEqual(true)
|
|
expect(approximatelyEqualHsv(rgbToHsv(255, 255, 0), [60, 1, 1])).toEqual(
|
|
true
|
|
)
|
|
expect(approximatelyEqualHsv(rgbToHsv(0, 255, 0), [120, 1, 1])).toEqual(
|
|
true
|
|
)
|
|
expect(approximatelyEqualHsv(rgbToHsv(0, 0, 255), [240, 1, 1])).toEqual(
|
|
true
|
|
)
|
|
expect(approximatelyEqualHsv(rgbToHsv(255, 0, 255), [300, 1, 1])).toEqual(
|
|
true
|
|
)
|
|
expect(
|
|
approximatelyEqualHsv(rgbToHsv(64, 128, 128), [180, 0.5, 0.5])
|
|
).toEqual(true)
|
|
expect(
|
|
approximatelyEqualHsv(rgbToHsv(193, 196, 224), [234, 0.14, 0.88])
|
|
).toEqual(true)
|
|
expect(
|
|
approximatelyEqualHsv(rgbToHsv(128, 32, 80), [330, 0.75, 0.5])
|
|
).toEqual(true)
|
|
})
|
|
})
|