Add utility function for string to rgba color conversion.

This commit is contained in:
kovacsv 2023-01-21 22:54:31 +01:00
parent 747ed18d20
commit b9fe936047
3 changed files with 32 additions and 2 deletions

View File

@ -44,7 +44,7 @@ import { ArrayBufferToUtf8String, ArrayBufferToAsciiString, AsciiStringToArrayBu
import { SetExternalLibLocation, GetExternalLibPath, LoadExternalLibrary } from './io/externallibs.js'; import { SetExternalLibLocation, GetExternalLibPath, LoadExternalLibrary } from './io/externallibs.js';
import { GetFileName, GetFileExtension, RequestUrl, ReadFile, TransformFileHostUrls, IsUrl, FileSource, FileFormat } from './io/fileutils.js'; import { GetFileName, GetFileExtension, RequestUrl, ReadFile, TransformFileHostUrls, IsUrl, FileSource, FileFormat } from './io/fileutils.js';
import { TextWriter } from './io/textwriter.js'; import { TextWriter } from './io/textwriter.js';
import { RGBColor, RGBAColor, ColorComponentFromFloat, ColorComponentToFloat, RGBColorFromFloatComponents, SRGBToLinear, LinearToSRGB, IntegerToHexString, RGBColorToHexString, RGBAColorToHexString, HexStringToRGBColor, ArrayToRGBColor, RGBColorIsEqual } from './model/color.js'; import { RGBColor, RGBAColor, ColorComponentFromFloat, ColorComponentToFloat, RGBColorFromFloatComponents, SRGBToLinear, LinearToSRGB, IntegerToHexString, RGBColorToHexString, RGBAColorToHexString, HexStringToRGBColor, HexStringToRGBAColor, ArrayToRGBColor, RGBColorIsEqual } from './model/color.js';
import { GeneratorParams, Generator, GeneratorHelper, GenerateCuboid, GenerateCone, GenerateCylinder, GenerateSphere, GeneratePlatonicSolid } from './model/generator.js'; import { GeneratorParams, Generator, GeneratorHelper, GenerateCuboid, GenerateCone, GenerateCylinder, GenerateSphere, GeneratePlatonicSolid } from './model/generator.js';
import { TextureMap, MaterialBase, FaceMaterial, PhongMaterial, PhysicalMaterial, TextureMapIsEqual, TextureIsEqual, MaterialType } from './model/material.js'; import { TextureMap, MaterialBase, FaceMaterial, PhongMaterial, PhysicalMaterial, TextureMapIsEqual, TextureIsEqual, MaterialType } from './model/material.js';
import { Mesh } from './model/mesh.js'; import { Mesh } from './model/mesh.js';
@ -213,6 +213,7 @@ export {
RGBColorToHexString, RGBColorToHexString,
RGBAColorToHexString, RGBAColorToHexString,
HexStringToRGBColor, HexStringToRGBColor,
HexStringToRGBAColor,
ArrayToRGBColor, ArrayToRGBColor,
RGBColorIsEqual, RGBColorIsEqual,
GeneratorParams, GeneratorParams,

View File

@ -157,6 +157,22 @@ export function HexStringToRGBColor (hexString)
return new RGBColor (r, g, b); return new RGBColor (r, g, b);
} }
export function HexStringToRGBAColor (hexString)
{
if (hexString.length !== 6 && hexString.length !== 8) {
return null;
}
let r = parseInt (hexString.substring (0, 2), 16);
let g = parseInt (hexString.substring (2, 4), 16);
let b = parseInt (hexString.substring (4, 6), 16);
let a = 255;
if (hexString.length === 8) {
a = parseInt (hexString.substring (6, 8), 16);
}
return new RGBAColor (r, g, b, a);
}
export function ArrayToRGBColor (arr) export function ArrayToRGBColor (arr)
{ {
return new RGBColor (arr[0], arr[1], arr[2]); return new RGBColor (arr[0], arr[1], arr[2]);

View File

@ -229,12 +229,25 @@ describe ('Color Conversion', function () {
assert.ok (!OV.RGBColorIsEqual (new OV.RGBColor (10, 20, 30), new OV.RGBColor (10, 20, 31))); assert.ok (!OV.RGBColorIsEqual (new OV.RGBColor (10, 20, 30), new OV.RGBColor (10, 20, 31)));
}); });
it ('Color hex string conversion', function () { it ('RGB Color hex string conversion', function () {
let color = new OV.RGBColor (10, 20, 30); let color = new OV.RGBColor (10, 20, 30);
let hexString = '0a141e'; let hexString = '0a141e';
assert.strictEqual (OV.RGBColorToHexString (color), hexString); assert.strictEqual (OV.RGBColorToHexString (color), hexString);
assert.deepStrictEqual (OV.HexStringToRGBColor (hexString), color); assert.deepStrictEqual (OV.HexStringToRGBColor (hexString), color);
}); });
it ('RGBA Color hex string conversion', function () {
let color = new OV.RGBAColor (10, 20, 30, 40);
let hexString = '0a141e28';
assert.strictEqual (OV.RGBAColorToHexString (color), hexString);
assert.deepStrictEqual (OV.HexStringToRGBAColor (hexString), color);
let color2 = new OV.RGBAColor (10, 20, 30, 255);
let hexString2 = '0a141e';
let hexString2a = '0a141eff';
assert.strictEqual (OV.RGBAColorToHexString (color2), hexString2a);
assert.deepStrictEqual (OV.HexStringToRGBAColor (hexString2), color2);
});
}); });
describe ('Node Hierarchy', function () { describe ('Node Hierarchy', function () {