Skip to content

Commit

Permalink
fix(common): assume hex is ARGB
Browse files Browse the repository at this point in the history
  • Loading branch information
sgtcoolguy committed Jul 6, 2020
1 parent 8783a06 commit daf8056
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion common/Resources/ti.internal/extensions/ti/ti.ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ if (!isIOS13Plus) {
// For now, return a string on iOS < 13, Android so we can pass the result directly to the UI property we want to set
// Otherwise we need to modify the Android APIs to accept this faked Ti.UI.Color instance and convert it to it's own internal
// Color representation
return colorObj.toRGBAString(); // rgba is standard across iOS/Android. Hex on Android ia ARGB vs RGBA on iOS.
return colorObj.toRGBAString(); // rgba is standard across iOS/Android
} catch (error) {
console.error(`Failed to lookup color for ${colorName}`);
}
Expand Down
22 changes: 15 additions & 7 deletions common/lib/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Color {
}

/**
* Used by IOS.
* Used by CSS.
* Converts this color to a hex string with leading '#' symbol and 6- or 8-
* hexadecimal characters (depending on if alpha is 1.0)
* @returns {string}
Expand All @@ -74,7 +74,7 @@ class Color {
}

/**
* Used by Android
* Used by Android/iOS
* Converts this color to a hex string with leading '#' symbol and 6- or 8-
* hexadecimal characters (depending on if alpha is 1.0). Alpha is the first entry (if there is alpha.)
* @returns {string}
Expand All @@ -86,6 +86,14 @@ class Color {
return `#${this.alphaHex()}${this._toRGBHexString()}`;
}

/**
* For commonality with native iOS TiColor proxy. Produces an AARRGGBB (or RRGGBB if full alpha) hex string
* @returns {string}
*/
toHex() {
return this.toARGBHexString();
}

/**
* Converts this color to an rgba expression. This expression is more consistent across platforms.
* (whereas iOS/Android differ in expecttaiosn for hex strings.)
Expand Down Expand Up @@ -123,10 +131,10 @@ class Color {
*/
static fromHex8String(hex) {
const startIndex = hex.startsWith('#') ? 1 : 0;
const r = parseInt(hex.substr(startIndex, 2), 16);
const g = parseInt(hex.substr(startIndex + 2, 2), 16);
const b = parseInt(hex.substr(startIndex + 4, 2), 16);
const alpha = parseInt(hex.substr(startIndex + 6, 2), 16); // alpha is now 0-255
const alpha = parseInt(hex.substr(startIndex, 2), 16); // alpha is now 0-255
const r = parseInt(hex.substr(startIndex + 2, 2), 16);
const g = parseInt(hex.substr(startIndex + 4, 2), 16);
const b = parseInt(hex.substr(startIndex + 6, 2), 16);
return new Color(r, g, b, alpha / 255.0); // convert to 0.0-1.0 (percent)
}

Expand Down Expand Up @@ -154,7 +162,7 @@ class Color {
color = color.replace(HEX_3_REGEX, (m, r, g, b) => r + r + g + g + b + b);
} else if (color.length === 4) {
// Expand shorthand form (e.g. "03F8") to full form (e.g. "0033FF88")
color = color.replace(HEX_4_REGEX, (m, r, g, b, a) => r + r + g + g + b + b + a + a);
color = color.replace(HEX_4_REGEX, (m, a, r, g, b) => a + a + r + r + g + g + b + b);
}

if (HEX_6_REGEX.exec(color)) {
Expand Down

0 comments on commit daf8056

Please sign in to comment.