Files
Stirling-PDF/frontend/src/hooks/tools/validateSignature/utils/pdfPalette.ts
T
EthanHealy01andAnthony Stirling a8573c99b7 V2 Validate PDF Signature tool (#4679)
# Description of Changes

<!--
Please provide a summary of the changes, including:

- What was changed
- Why the change was made
- Any challenges encountered

Closes #(issue_number)
-->

---

## Checklist

### General

- [ ] I have read the [Contribution
Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md)
- [ ] I have read the [Stirling-PDF Developer
Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md)
(if applicable)
- [ ] I have read the [How to add new languages to
Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md)
(if applicable)
- [ ] I have performed a self-review of my own code
- [ ] My changes generate no new warnings

### Documentation

- [ ] I have updated relevant docs on [Stirling-PDF's doc
repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/)
(if functionality has heavily changed)
- [ ] I have read the section [Add New Translation
Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md#add-new-translation-tags)
(for new translation tags only)

### UI Changes (if applicable)

- [ ] Screenshots or videos demonstrating the UI changes are attached
(e.g., as comments or direct attachments in the PR)

### Testing (if applicable)

- [ ] I have tested my changes locally. Refer to the [Testing
Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md#6-testing)
for more details.

---------

Co-authored-by: Anthony Stirling <77850077+Frooodle@users.noreply.github.com>
2025-10-16 13:45:59 +01:00

61 lines
2.3 KiB
TypeScript

import { rgb } from 'pdf-lib';
type RgbTuple = [number, number, number];
const defaultLightPalette: Record<
'headerBackground' | 'accent' | 'textPrimary' | 'textMuted' | 'boxBackground' | 'boxBorder' | 'warning' | 'danger' | 'success' | 'neutral',
RgbTuple
> = {
headerBackground: [239, 246, 255],
accent: [59, 130, 246],
textPrimary: [30, 41, 59],
textMuted: [100, 116, 139],
boxBackground: [248, 250, 252],
boxBorder: [226, 232, 240],
warning: [234, 179, 8],
danger: [248, 113, 113],
success: [34, 197, 94],
neutral: [148, 163, 184],
};
const toRgb = ([r, g, b]: RgbTuple) => rgb(r / 255, g / 255, b / 255);
/**
* Utility function to get CSS variable values and convert them to pdf-lib RGB format.
* Falls back to sensible defaults when the CSS variable cannot be resolved.
*/
function getCssVariableAsRgb(variableName: string, fallback: RgbTuple) {
if (typeof window === 'undefined') {
return toRgb(fallback);
}
const value = getComputedStyle(document.documentElement).getPropertyValue(variableName).trim();
if (!value) {
console.warn(`CSS variable ${variableName} not found, using fallback`);
return toRgb(fallback);
}
const [r, g, b] = value.split(' ').map(Number);
if ([r, g, b].some((component) => Number.isNaN(component))) {
console.warn(`Invalid CSS variable format for ${variableName}: ${value}`);
return toRgb(fallback);
}
return rgb(r / 255, g / 255, b / 255);
}
export const colorPalette = {
headerBackground: getCssVariableAsRgb('--pdf-light-header-bg', defaultLightPalette.headerBackground),
accent: getCssVariableAsRgb('--pdf-light-accent', defaultLightPalette.accent),
textPrimary: getCssVariableAsRgb('--pdf-light-text-primary', defaultLightPalette.textPrimary),
textMuted: getCssVariableAsRgb('--pdf-light-text-muted', defaultLightPalette.textMuted),
boxBackground: getCssVariableAsRgb('--pdf-light-box-bg', defaultLightPalette.boxBackground),
boxBorder: getCssVariableAsRgb('--pdf-light-box-border', defaultLightPalette.boxBorder),
warning: getCssVariableAsRgb('--pdf-light-warning', defaultLightPalette.warning),
danger: getCssVariableAsRgb('--pdf-light-danger', defaultLightPalette.danger),
success: getCssVariableAsRgb('--pdf-light-success', defaultLightPalette.success),
neutral: getCssVariableAsRgb('--pdf-light-neutral', defaultLightPalette.neutral),
};