Skip to content

Commit

Permalink
fix(android): semantic colors with alpha value
Browse files Browse the repository at this point in the history
refs TIMOB-27519
  • Loading branch information
drauggres committed Nov 5, 2019
1 parent 1365850 commit c336b95
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions common/Resources/ti.internal/extensions/ti/ti.ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

let colorset;
let osVersion;
const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
const fallbackColor = 'black'; // To match iphone/Classes/TiUIiOSProxy.m#fetchSemanticColor

// As Android passes a new instance of Ti.UI to every JS file we can't just
// Ti.UI within this file, we must call kroll.binding to get the Titanium
Expand All @@ -32,6 +34,20 @@ Object.defineProperty(uiModule, 'semanticColorType', {
}
});

function hexToRgb(hex) {
let alpha = 1;
let color = hex;
if (hex.color) {
alpha = hex.alpha / 100; // convert from 0-100 range to 0-1 range
color = hex.color;
}
// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
color = color.replace(shorthandRegex, (m, r, g, b) => r + r + g + g + b + b);

const r = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(color);
return r ? `rgba(${parseInt(r[1], 16)}, ${parseInt(r[2], 16)}, ${parseInt(r[3], 16)}, ${alpha.toFixed(3)})` : null;
};

uiModule.fetchSemanticColor = function fetchSemanticColor (colorName) {
if (!osVersion) {
osVersion = parseInt(Ti.Platform.version.split('.')[0]);
Expand All @@ -51,10 +67,22 @@ uiModule.fetchSemanticColor = function fetchSemanticColor (colorName) {
return;
}
}
try {
return colorset[colorName][uiModule.semanticColorType].color || colorset[colorName][uiModule.semanticColorType];
} catch (error) {
console.error(`Failed to lookup color for ${colorName}`);
if (!colorset[colorName]) {
return fallbackColor;
}
const color = colorset[colorName][Ti.UI.semanticColorType];
if (typeof color === 'string') {
return color;
}
if (!color || typeof color !== 'object') {
return fallbackColor;
}
if (color['color'] && color['alpha']) {
const result = hexToRgb(color);
if (result) {
return result;
}
}
return fallbackColor;
}
};

0 comments on commit c336b95

Please sign in to comment.