Fixes #24566: Add support for three char hex colors in syntax colors
This commit is contained in:
parent
1798af5363
commit
8f67f0721c
6
npm-shrinkwrap.json
generated
6
npm-shrinkwrap.json
generated
|
@ -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",
|
||||
|
|
|
@ -37,7 +37,7 @@
|
|||
"v8-profiler": "jrieken/v8-profiler#vscode",
|
||||
"vscode-debugprotocol": "1.18.0",
|
||||
"vscode-ripgrep": "0.0.11",
|
||||
"vscode-textmate": "^3.1.3",
|
||||
"vscode-textmate": "^3.1.4",
|
||||
"winreg": "1.2.0",
|
||||
"xterm": "Tyriar/xterm.js#vscode-release/1.11",
|
||||
"yauzl": "2.3.1"
|
||||
|
|
|
@ -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 () {
|
||||
|
|
Loading…
Reference in a new issue