提交 8f67f072 编写于 作者: A Alex Dima

Fixes #24566: Add support for three char hex colors in syntax colors

上级 1798af53
......@@ -420,9 +420,9 @@
"resolved": "https://registry.npmjs.org/vscode-ripgrep/-/vscode-ripgrep-0.0.11.tgz"
},
"vscode-textmate": {
"version": "3.1.3",
"from": "vscode-textmate@3.1.3",
"resolved": "https://registry.npmjs.org/vscode-textmate/-/vscode-textmate-3.1.3.tgz"
"version": "3.1.4",
"from": "vscode-textmate@3.1.4",
"resolved": "https://registry.npmjs.org/vscode-textmate/-/vscode-textmate-3.1.4.tgz"
},
"windows-foreground-love": {
"version": "0.1.0",
......
......@@ -113,14 +113,27 @@ function hex2rgba(hex: string): RGBA {
// Invalid color
return null;
}
if (hex.length === 7 && hex.charCodeAt(0) === CharCode.Hash) {
const length = hex.length;
if (length === 0) {
// Invalid color
return null;
}
if (hex.charCodeAt(0) !== CharCode.Hash) {
// Does not begin with a #
return null;
}
if (length === 7) {
// #RRGGBB format
const r = 16 * _parseHexDigit(hex.charCodeAt(1)) + _parseHexDigit(hex.charCodeAt(2));
const g = 16 * _parseHexDigit(hex.charCodeAt(3)) + _parseHexDigit(hex.charCodeAt(4));
const b = 16 * _parseHexDigit(hex.charCodeAt(5)) + _parseHexDigit(hex.charCodeAt(6));
return new RGBA(r, g, b, 255);
}
if (hex.length === 9 && hex.charCodeAt(0) === CharCode.Hash) {
if (length === 9) {
// #RRGGBBAA format
const r = 16 * _parseHexDigit(hex.charCodeAt(1)) + _parseHexDigit(hex.charCodeAt(2));
const g = 16 * _parseHexDigit(hex.charCodeAt(3)) + _parseHexDigit(hex.charCodeAt(4));
......@@ -128,12 +141,50 @@ function hex2rgba(hex: string): RGBA {
const a = 16 * _parseHexDigit(hex.charCodeAt(7)) + _parseHexDigit(hex.charCodeAt(8));
return new RGBA(r, g, b, a);
}
if (length === 4) {
// #RGB format
const r = _parseHexDigit(hex.charCodeAt(1));
const g = _parseHexDigit(hex.charCodeAt(2));
const b = _parseHexDigit(hex.charCodeAt(3));
return new RGBA(16 * r + r, 16 * g + g, 16 * b + b);
}
if (length === 5) {
// #RGBA format
const r = _parseHexDigit(hex.charCodeAt(1));
const g = _parseHexDigit(hex.charCodeAt(2));
const b = _parseHexDigit(hex.charCodeAt(3));
const a = _parseHexDigit(hex.charCodeAt(4));
return new RGBA(16 * r + r, 16 * g + g, 16 * b + b, 16 * a + a);
}
// Invalid color
return null;
}
export function isValidHexColor(hex: string): boolean {
return /^#[0-9a-f]{6}([0-9a-f]{2})?$/i.test(hex);
if (/^#[0-9a-f]{6}$/i.test(hex)) {
// #rrggbb
return true;
}
if (/^#[0-9a-f]{8}$/i.test(hex)) {
// #rrggbbaa
return true;
}
if (/^#[0-9a-f]{3}$/i.test(hex)) {
// #rgb
return true;
}
if (/^#[0-9a-f]{4}$/i.test(hex)) {
// #rgba
return true;
}
return false;
}
function _parseHexDigit(charCode: CharCode): number {
......
......@@ -107,6 +107,9 @@ suite('Color', () => {
assertParseColor('#0f0F0f', new RGBA(15, 15, 15, 255));
assertParseColor('#a0A0a0', new RGBA(160, 160, 160, 255));
assertParseColor('#FFFFFF', new RGBA(255, 255, 255, 255));
assertParseColor('#CFA', new RGBA(204, 255, 170, 255));
assertParseColor('#CFA8', new RGBA(204, 255, 170, 136));
});
test('isValidHexColor', function () {
......@@ -134,6 +137,9 @@ suite('Color', () => {
assert.equal(isValidHexColor('#0f0F0f'), true);
assert.equal(isValidHexColor('#a0A0a0'), true);
assert.equal(isValidHexColor('#FFFFFF'), true);
assert.equal(isValidHexColor('#CFA'), true);
assert.equal(isValidHexColor('#CFAF'), true);
});
test('isLighterColor', function () {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册