Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add support for engine ARGB colors #1033

Merged
merged 4 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apis/nucleus/src/utils/__tests__/background-props.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('Background property resolver', () => {
bgCompLayout = {
key: 'general',
bgColor: {
useColorExpression: true,
useExpression: true,
colorExpression: '#ff0000',
color: { index: -1, color: 'aqua' },
},
Expand All @@ -40,7 +40,7 @@ describe('Background property resolver', () => {
expect(color).toBe('rgb(255, 0, 0)');
});
test('should resolve background color by picker', () => {
bgCompLayout.bgColor.useColorExpression = false;
bgCompLayout.bgColor.useExpression = false;
const color = resolveBgColor(bgCompLayout, t);
expect(color).toBe('aqua');
});
Expand Down
2 changes: 1 addition & 1 deletion apis/nucleus/src/utils/background-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const resolveBgImage = (bgComp, app) => {
export const resolveBgColor = (bgComp, theme) => {
const bgColor = bgComp?.bgColor;
if (bgColor && theme) {
if (bgColor.useColorExpression) {
if (bgColor.useExpression) {
return theme.validateColor(bgColor.colorExpression);
}
return bgColor.color && bgColor.color.color !== 'none' ? theme.getColorPickerColor(bgColor.color, true) : undefined;
Expand Down
20 changes: 20 additions & 0 deletions apis/theme/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,36 @@ export default function theme() {
/**
* Validates a color string using d3-color.
* See https://www.npmjs.com/package/d3-color
* Additionally supports the non-standard engine
* format ARGB(0-255,0-255,0-255,0-255)
* @param {string} specifier
* @returns {string|undefined} The resolved color or undefined
* @ignore
*
* @example
* theme.validateColor("red"); // returns "rgba(255,0,0,1)"
* theme.validateColor("#00ff00"); // returns "rgba(0,255,0,1)"
* theme.validateColor("ARGB(102,255,50,100)"); // returns "rgba(255,50,100,0.4)"
* theme.validateColor("FOO"); // returns undefined
*/
validateColor(...args) {
/* Added this to support the non-standard ARGB format from engine */
const colorString = args[0];
let matches;
/* eslint-disable no-cond-assign */
if (
typeof colorString === 'string' &&
(matches = /^ARGB\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$/i.exec(colorString))
) {
// ARGB(255,255,255,255)
const a = parseInt(matches[1], 10) / 255;
const r = parseInt(matches[2], 10);
const g = parseInt(matches[3], 10);
const b = parseInt(matches[4], 10);
return `rgba(${r},${g},${b},${a})`;
}
/* eslint-enable no-cond-assign */

const c = d3color(...args);
return c ? c.toString() : undefined;
},
Expand Down