Skip to content

Instantly share code, notes, and snippets.

@vadiole
Last active September 12, 2022 15:19
Show Gist options
  • Save vadiole/f42dab237e192728481531e20315b0c4 to your computer and use it in GitHub Desktop.
Save vadiole/f42dab237e192728481531e20315b0c4 to your computer and use it in GitHub Desktop.
Converts json colors exported from figma into compose
// Colors from https://www.figma.com/community/plugin/1143682832255826428
const colors = {
colorName: 'rgb(23, 23, 23)',
};
for (var key in colors) {
if (colors.hasOwnProperty(key)) {
console.log('val ' + key + ' = Color(' + RGBAToAHex(colors[key]) + ')');
}
}
function RGBAToAHex(rgba) {
let color = rgba
.replace(/^rgba?\(|\s+|\)$/g, '') // Get's rgba / rgb string values
.split(',') // Splits them at ","
.map(string => parseFloat(string)) // Converts them to numbers
.map((number, index) => (index === 3 ? Math.round(number * 255) : number)) // Converts alpha to 255 number
.map(number => number.toString(16)) // Converts numbers to hex
.map(string => (string.length === 1 ? '0' + string : string)); // Adds 0 when length of one number is 1
if (color.length === 3) {
color.push('FF'); // Add alpha if missing
}
color.unshift(color.pop()); // Move alpha to the start
return '0x' + color.join('').toUpperCase();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment