@colormate/core
Version:
A modular and efficient color management library for JavaScript and TypeScript applications.
22 lines (21 loc) • 748 B
JavaScript
// RgbToHex.ts
import { ColorValidator } from '@validators/ColorValidator';
import { ColorConverter } from '@helpers/ColorConverter';
export class RgbToHex extends ColorConverter {
static convertRgbToHex(rgb) {
return new RgbToHex().convert(rgb);
}
convert(rgb) {
if (!ColorValidator.isValidRgb(rgb)) {
throw new Error('Invalid RGB format');
}
const cacheKey = `${rgb.r},${rgb.g},${rgb.b}`;
const cachedResult = this.getCachedResult(cacheKey);
if (cachedResult)
return cachedResult;
const hex = '#' +
((1 << 24) + (rgb.r << 16) + (rgb.g << 8) + rgb.b).toString(16).slice(1);
this.cacheResult(cacheKey, hex);
return hex;
}
}