Skip to content

Commit d3a80dc

Browse files
committed
Using Color.fromHex with invalid input should return null
1 parent eae6de3 commit d3a80dc

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

src/vs/vscode.proposed.d.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,12 @@ declare module 'vscode' {
126126
*/
127127
static fromHSLA(hue: number, saturation: number, luminance: number, alpha: number): Color;
128128

129-
// TODO: this isn't needed. this is a parsing util. remove
130-
static fromHex(hex: string): Color;
129+
/**
130+
* Creates a color by from a hex string. Supported formats are: #RRGGBB, #RRGGBBAA, #RGB, #RGBA.
131+
* <code>null</code> is returned if the string does not match one of the supported formats.
132+
* @param hex a string to parse
133+
*/
134+
static fromHex(hex: string): Color | null;
131135
}
132136

133137
/**

src/vs/workbench/api/node/extHostTypes.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,9 +1032,13 @@ export class Color {
10321032
return new Color(color.r, color.g, color.b, color.a / 255);
10331033
}
10341034

1035-
static fromHex(hex: string): Color {
1036-
const color = BaseColor.fromHex(hex).rgba;
1037-
return new Color(color.r, color.g, color.b, color.a / 255);
1035+
static fromHex(hex: string): Color | null {
1036+
let baseColor = BaseColor.Format.CSS.parseHex(hex);
1037+
if (baseColor) {
1038+
const rgba = baseColor.rgba;
1039+
return new Color(rgba.r, rgba.g, rgba.b, rgba.a / 255);
1040+
}
1041+
return null;
10381042
}
10391043
}
10401044

0 commit comments

Comments
 (0)