Skip to content

Commit

Permalink
Move hextocolor to utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
leeyi45 committed Oct 23, 2024
1 parent 233dd39 commit 5e94ed4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 29 deletions.
3 changes: 1 addition & 2 deletions src/bundles/csg/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ import {
is_list
} from 'js-slang/dist/stdlib/list';
import save from 'save-file';
import { degreesToRadians } from '../../common/utilities';
import { degreesToRadians, hexToColor } from '../../common/utilities';
import { Core } from './core';
import type { Solid } from './jscad/types';
import {
Group,
Shape,
hexToColor,
type Operable,
type RenderGroup,
centerPrimitive
Expand Down
16 changes: 1 addition & 15 deletions src/bundles/csg/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
scale as _scale,
translate as _translate
} from '@jscad/modeling/src/operations/transforms';
import { hexToColor } from '../../common/utilities';
import type { ReplResult } from '../../typings/type_helpers';
import { Core } from './core';
import type { AlphaColor, Color, Solid } from './jscad/types';
Expand Down Expand Up @@ -218,21 +219,6 @@ export function centerPrimitive(shape: Shape) {
return new Shape(solid);
}

export function hexToColor(hex: string): Color {
const regex: RegExp
= /^#?(?<red>[\da-f]{2})(?<green>[\da-f]{2})(?<blue>[\da-f]{2})$/iu;
const potentialGroups: { [key: string]: string } | undefined
= hex.match(regex)?.groups;
if (potentialGroups === undefined) return [0, 0, 0];
const groups: { [key: string]: string } = potentialGroups;

return [
parseInt(groups.red, 16) / 0xff,
parseInt(groups.green, 16) / 0xff,
parseInt(groups.blue, 16) / 0xff
];
}

export function colorToAlphaColor(
color: Color,
opacity: number = 1
Expand Down
15 changes: 3 additions & 12 deletions src/bundles/rune/runes_ops.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* This file contains the bundle's private functions for runes.
*/
import { hexToColor as hexToColorUtil } from '../../common/utilities';
import { Rune } from './rune';

// =============================================================================
Expand Down Expand Up @@ -338,18 +339,8 @@ export const colorPalette = [
];

export function hexToColor(hex: string): number[] {
const result = /^#?(?<red>[a-f\d]{2})(?<green>[a-f\d]{2})(?<blue>[a-f\d]{2})$/iu.exec(
hex
);
if (result === null || result.length < 4) {
return [0, 0, 0];
}
return [
parseInt(result[1], 16) / 255,
parseInt(result[2], 16) / 255,
parseInt(result[3], 16) / 255,
1
];
const result = hexToColorUtil(hex);
return [ ...result, 1];
}

export function addColorFromHex(rune: Rune, hex: string) {
Expand Down
17 changes: 17 additions & 0 deletions src/common/__tests__/hextocolor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { hexToColor } from '../utilities';

describe('Test hexToColor', () => {
test.each([
['#FFFFFF', [1, 1, 1]],
['ffffff', [1, 1, 1]],
['0088ff', [0, 0.53, 1]],
['#000000', [0, 0, 0]],
['#GGGGGG', [0, 0, 0]],
['888888', [0.53, 0.53, 0.53]]
])('Testing %s', (c, expected) => {
const result = hexToColor(c);
for (let i = 0; i < expected.length; i++) {
expect(result[i]).toBeCloseTo(expected[i]);
}
});
});
12 changes: 12 additions & 0 deletions src/common/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,15 @@
export function degreesToRadians(degrees: number): number {
return (degrees / 360) * (2 * Math.PI);
}

export function hexToColor(hex: string): [number, number, number] {
const regex = /^#?([\da-f]{2})([\da-f]{2})([\da-f]{2})$/igu;
const groups = regex.exec(hex);

if (groups == undefined) return [0, 0, 0];
return [
parseInt(groups[1], 16) / 0xff,
parseInt(groups[2], 16) / 0xff,
parseInt(groups[3], 16) / 0xff
];
}

0 comments on commit 5e94ed4

Please sign in to comment.