mirror of
https://github.com/konvajs/konva.git
synced 2025-04-05 20:48:28 +08:00
fix: 🐛 hex color string supports four/eight-value syntax
This commit is contained in:
parent
763a7be0f9
commit
0744aac409
24
src/Util.ts
24
src/Util.ts
@ -609,7 +609,9 @@ export const Util = {
|
|||||||
return (
|
return (
|
||||||
Util._namedColorToRBA(str) ||
|
Util._namedColorToRBA(str) ||
|
||||||
Util._hex3ColorToRGBA(str) ||
|
Util._hex3ColorToRGBA(str) ||
|
||||||
|
Util._hex4ColorToRGBA(str) ||
|
||||||
Util._hex6ColorToRGBA(str) ||
|
Util._hex6ColorToRGBA(str) ||
|
||||||
|
Util._hex8ColorToRGBA(str) ||
|
||||||
Util._rgbColorToRGBA(str) ||
|
Util._rgbColorToRGBA(str) ||
|
||||||
Util._rgbaColorToRGBA(str) ||
|
Util._rgbaColorToRGBA(str) ||
|
||||||
Util._hslColorToRGBA(str)
|
Util._hslColorToRGBA(str)
|
||||||
@ -659,6 +661,17 @@ export const Util = {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
// Parse #nnnnnnnn
|
||||||
|
_hex8ColorToRGBA(str: string): RGBA {
|
||||||
|
if (str[0] === '#' && str.length === 9) {
|
||||||
|
return {
|
||||||
|
r: parseInt(str.slice(1, 3), 16),
|
||||||
|
g: parseInt(str.slice(3, 5), 16),
|
||||||
|
b: parseInt(str.slice(5, 7), 16),
|
||||||
|
a: parseInt(str.slice(7, 9), 16) / 0xff,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
},
|
||||||
// Parse #nnnnnn
|
// Parse #nnnnnn
|
||||||
_hex6ColorToRGBA(str: string): RGBA {
|
_hex6ColorToRGBA(str: string): RGBA {
|
||||||
if (str[0] === '#' && str.length === 7) {
|
if (str[0] === '#' && str.length === 7) {
|
||||||
@ -670,6 +683,17 @@ export const Util = {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
// Parse #nnnn
|
||||||
|
_hex4ColorToRGBA(str: string): RGBA {
|
||||||
|
if (str[0] === '#' && str.length === 5) {
|
||||||
|
return {
|
||||||
|
r: parseInt(str[1] + str[1], 16),
|
||||||
|
g: parseInt(str[2] + str[2], 16),
|
||||||
|
b: parseInt(str[3] + str[3], 16),
|
||||||
|
a: parseInt(str[4] + str[4], 16) / 0xff,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
},
|
||||||
// Parse #nnn
|
// Parse #nnn
|
||||||
_hex3ColorToRGBA(str: string): RGBA {
|
_hex3ColorToRGBA(str: string): RGBA {
|
||||||
if (str[0] === '#' && str.length === 4) {
|
if (str[0] === '#' && str.length === 4) {
|
||||||
|
@ -76,6 +76,43 @@ describe('Util', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('colorToRGBA() - from hex color string with percentage to RGBA conversion!', function () {
|
||||||
|
assert.deepEqual(Konva.Util.colorToRGBA('#F00'), {
|
||||||
|
r: 255,
|
||||||
|
g: 0,
|
||||||
|
b: 0,
|
||||||
|
a: 1,
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.deepEqual(Konva.Util.colorToRGBA('#F00F'), {
|
||||||
|
r: 255,
|
||||||
|
g: 0,
|
||||||
|
b: 0,
|
||||||
|
a: 1,
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.deepEqual(Konva.Util.colorToRGBA('#F00C'), {
|
||||||
|
r: 255,
|
||||||
|
g: 0,
|
||||||
|
b: 0,
|
||||||
|
a: 0.8,
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.deepEqual(Konva.Util.colorToRGBA('#FF0000FF'), {
|
||||||
|
r: 255,
|
||||||
|
g: 0,
|
||||||
|
b: 0,
|
||||||
|
a: 1,
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.deepEqual(Konva.Util.colorToRGBA('#FF0000CC'), {
|
||||||
|
r: 255,
|
||||||
|
g: 0,
|
||||||
|
b: 0,
|
||||||
|
a: 0.8,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('make sure Transform is exported', () => {
|
it('make sure Transform is exported', () => {
|
||||||
assert.equal(!!Konva.Transform, true);
|
assert.equal(!!Konva.Transform, true);
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user