Skip to content

Commit

Permalink
feature: add colorHSV2RGB
Browse files Browse the repository at this point in the history
  • Loading branch information
0b5vr committed Oct 7, 2022
1 parent c275019 commit c6dc8c5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/color/colorHSV2RGB.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { RawRGB } from './RawRGB';
import { lerp } from '../math/utils';

/**
* Convert a color from HSV to RGB.
* Each component of the HSV must be given in [0 - 1] range.
*
* Ref: https://en.wikipedia.org/wiki/HSV_color_space
*/
export function colorHSV2RGB( [ h, s, v ]: [ number, number, number ] ): RawRGB {
const ht = h % 1.0 * 6.0;

return [ 0.0, 4.0, 2.0 ].map( ( p ) => {
const colH = Math.min( Math.max( (
Math.abs( ( ht + p ) % 6.0 - 3.0 ) - 1.0
), 0.0 ), 1.0 );
const colS = lerp( 1.0, colH, s );
return v * colS;
} ) as RawRGB;
}
1 change: 1 addition & 0 deletions src/color/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './colorFromAtariST';
export * from './colorHSV2RGB';
export * from './colorToHex';
export * from './colorTurbo';
export * from './eotfRec709';
Expand Down
14 changes: 14 additions & 0 deletions src/color/tests/colorHSV2RGB.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import '../../tests/matchers/toBeCloseToArray';
import { colorHSV2RGB } from '../colorHSV2RGB';

describe( 'colorHSV2RGB', () => {
it( 'converts the input HSV into RGB', () => {
const subject = colorHSV2RGB( [ 152.0 / 360.0, 0.59, 0.86 ] );

expect( subject ).toBeCloseToArray( [
90.0 / 255.0,
219.0 / 255.0,
159.0 / 255.0,
] );
} );
} );

0 comments on commit c6dc8c5

Please sign in to comment.