Skip to content

Commit

Permalink
Merge pull request #4 from alanhchoi/master
Browse files Browse the repository at this point in the history
convert argb hex code for color attributes
  • Loading branch information
seanghay authored May 24, 2023
2 parents db0f765 + 5c2dc63 commit 4d2d5d2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
24 changes: 24 additions & 0 deletions src/vector-drawable-svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const attributesMap = {

const attributeTransforms = {
'android:fillType': (value) => value && value.toLowerCase(),
'android:fillColor': convertHexColor,
'android:strokeColor': convertHexColor,
}

const groupAttrsMap = {
Expand Down Expand Up @@ -175,6 +177,28 @@ function removeDimenSuffix(dimen) {
return dimen;
}

function convertHexColor(argb) {
const digits = argb.replace(/^#/, '');

if (digits.length !== 4 && digits.length !== 8) {
return argb;
}

let red, green, blue, alpha;
if (digits.length === 4) {
alpha = digits[0];
red = digits[1];
green = digits[2];
blue = digits[3];
} else {
alpha = digits.substr(0, 2);
red = digits.substr(2, 2);
green = digits.substr(4, 2);
blue = digits.substr(6, 2);
}
return '#' + red + green + blue + alpha;
}


function transform(content, options) {

Expand Down
10 changes: 10 additions & 0 deletions tests/drawables/sample-02.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector android:height="24dp" android:viewportHeight="48"
android:viewportWidth="48" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#E02877ED" android:pathData="M24,0L24,0A24,24 0,0 1,48 24L48,24A24,24 0,0 1,24 48L24,48A24,24 0,0 1,0 24L0,24A24,24 0,0 1,24 0z"/>
<path android:fillColor="#fff"
android:pathData="M13.871,19.092v-1.9c0,-2.313 0.7,-3.009 3.009,-3.009h1.87L20.449,12.469L16.88,12.469C13.621,12.469 12.159,13.931 12.159,17.19L12.159,20.816ZM13.871,19.092v-1.9c0,-2.313 0.7,-3.009 3.009,-3.009h1.87L20.449,12.469L16.88,12.469C13.621,12.469 12.159,13.931 12.159,17.19L12.159,20.816ZM29.34,14.181h1.706c2.313,0 3.009,0.7 3.009,3.009L34.055,19.042l1.712,1.767L35.767,17.19c0,-3.259 -1.462,-4.721 -4.721,-4.721L27.681,12.469ZM29.34,14.181h1.706c2.313,0 3.009,0.7 3.009,3.009L34.055,19.042l1.712,1.767L35.767,17.19c0,-3.259 -1.462,-4.721 -4.721,-4.721L27.681,12.469ZM34.054,29.642l1.712,-1.668v3.377c0,3.259 -1.462,4.721 -4.721,4.721h-3.59l1.756,-1.712h1.834c2.313,0 3.009,-0.7 3.009,-3.009ZM35.766,27.974v3.377c0,3.259 -1.462,4.721 -4.721,4.721h-3.59l1.756,-1.712h1.834c2.313,0 3.009,-0.7 3.009,-3.009L34.054,29.642ZM18.708,34.364l1.74,1.712L16.88,36.076c-3.259,0 -4.721,-1.462 -4.721,-4.721L12.159,27.924l1.712,1.683v1.748c0,2.313 0.7,3.009 3.009,3.009ZM20.448,36.076L16.88,36.076c-3.259,0 -4.721,-1.462 -4.721,-4.721L12.159,27.924l1.712,1.683v1.748c0,2.313 0.7,3.009 3.009,3.009L18.708,34.364Z"
android:strokeColor="#00000000" android:strokeWidth="1"/>
<path android:fillColor="#BDFFFFFF"
android:pathData="M11.034,25.128L11.034,23.417L36.909,23.417L36.909,25.128Z"
android:strokeAlpha="0.747" android:strokeColor="#00000000" android:strokeWidth="1"/>
</vector>
4 changes: 0 additions & 4 deletions tests/simple-conversion.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const { transform } = require('..');
const path = require('path');
const fs = require('fs');
const xmlcompare = require('dom-compare').compare;


const vectorDrawablesDir = path.join(__dirname, 'drawables');
const svgsDir = path.join(__dirname, 'svgs');
Expand All @@ -23,5 +21,3 @@ fs.readdirSync(vectorDrawablesDir).forEach(filename => {
expect(svgContent).toBe(outputSVG);
});
})


1 change: 1 addition & 0 deletions tests/svgs/sample-02.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4d2d5d2

Please sign in to comment.