diff --git a/extensions/css/client/src/colorDecorators.ts b/extensions/css/client/src/colorDecorators.ts index 41af8690cd7..7c36b0f763c 100644 --- a/extensions/css/client/src/colorDecorators.ts +++ b/extensions/css/client/src/colorDecorators.ts @@ -5,7 +5,7 @@ 'use strict'; import * as parse from 'parse-color'; -import { window, workspace, DecorationOptions, DecorationRenderOptions, Disposable, Range, TextDocument, DocumentColorProvider, Color, ColorFormat, ColorRange } from 'vscode'; +import { window, workspace, DecorationOptions, DecorationRenderOptions, Disposable, Range, TextDocument, DocumentColorProvider, Color, ColorRange } from 'vscode'; const MAX_DECORATORS = 500; @@ -144,16 +144,6 @@ const CSSColorFormats = { } }; -function detectFormat(value: string): ColorFormat { - if (/^rgb/i.test(value)) { - return CSSColorFormats.RGB; - } else if (/^hsl/i.test(value)) { - return CSSColorFormats.HSL; - } else { - return CSSColorFormats.Hex; - } -} - export class ColorProvider implements DocumentColorProvider { constructor(private decoratorProvider: (uri: string) => Thenable) { } @@ -174,8 +164,7 @@ export class ColorProvider implements DocumentColorProvider { } } if (color) { - const format = detectFormat(value); - result.push(new ColorRange(range, color, format, [CSSColorFormats.Hex, CSSColorFormats.RGB, CSSColorFormats.HSL])); + result.push(new ColorRange(range, color, [CSSColorFormats.Hex, CSSColorFormats.RGB, CSSColorFormats.HSL])); } } return result; diff --git a/extensions/json/client/src/colorDecorators.ts b/extensions/json/client/src/colorDecorators.ts index 575fd5ff1bf..a01024f9c14 100644 --- a/extensions/json/client/src/colorDecorators.ts +++ b/extensions/json/client/src/colorDecorators.ts @@ -149,7 +149,7 @@ export class ColorProvider implements DocumentColorProvider { let color = Color.fromHex(value); if (color) { let r = new Range(range.start.line, range.start.character + 1, range.end.line, range.end.character - 1); - result.push(new ColorRange(r, color, ColorFormat_HEX, [ColorFormat_HEX])); + result.push(new ColorRange(r, color, [ColorFormat_HEX])); } } return result; diff --git a/src/vs/editor/common/modes.ts b/src/vs/editor/common/modes.ts index 26db1d15d4d..1d7de9e9a38 100644 --- a/src/vs/editor/common/modes.ts +++ b/src/vs/editor/common/modes.ts @@ -702,9 +702,6 @@ export interface IColorRange { */ color: IColor; - // TODO@joao TODO@michel can we drop this? - format: IColorFormat; - /** * The available formats for this specific color. */ diff --git a/src/vs/editor/contrib/colorPicker/browser/colorDetector.ts b/src/vs/editor/contrib/colorPicker/browser/colorDetector.ts index 4f7ce662043..656740b47cd 100644 --- a/src/vs/editor/contrib/colorPicker/browser/colorDetector.ts +++ b/src/vs/editor/contrib/colorPicker/browser/colorDetector.ts @@ -111,7 +111,6 @@ export class ColorDetector implements IEditorContribution { const colorRanges = colorInfos.map(c => ({ range: c.range, color: c.color, - format: c.format, availableFormats: c.availableFormats })); diff --git a/src/vs/editor/contrib/colorPicker/browser/colorPickerModel.ts b/src/vs/editor/contrib/colorPicker/browser/colorPickerModel.ts index 9bc32a4ab60..e055a02b3d2 100644 --- a/src/vs/editor/contrib/colorPicker/browser/colorPickerModel.ts +++ b/src/vs/editor/contrib/colorPicker/browser/colorPickerModel.ts @@ -23,17 +23,15 @@ export class ColorPickerModel { this._color = color; - if (!this._formatter.canFormatColor(color)) { + if (!this.formatter.canFormatColor(color)) { this.selectNextColorFormat(); } this._onDidChangeColor.fire(color); } - private _formatter: IColorFormatter; - get formatter(): IColorFormatter { return this._formatter; } + get formatter(): IColorFormatter { return this.formatters[this.formatterIndex]; } - private formatterIndex = 0; readonly formatters: IColorFormatter[]; private _onDidChangeColor = new Emitter(); @@ -42,25 +40,27 @@ export class ColorPickerModel { private _onDidChangeFormatter = new Emitter(); readonly onDidChangeFormatter: Event = this._onDidChangeFormatter.event; - constructor(color: Color, formatter: IColorFormatter, availableFormatters: IColorFormatter[]) { + constructor(color: Color, availableFormatters: IColorFormatter[], private formatterIndex: number) { if (availableFormatters.length === 0) { throw new Error('Color picker needs formats'); } + if (formatterIndex < 0 || formatterIndex >= availableFormatters.length) { + throw new Error('Formatter index out of bounds'); + } + this.originalColor = color; this.formatters = availableFormatters; - this._formatter = formatter; this._color = color; } selectNextColorFormat(): void { this.formatterIndex = (this.formatterIndex + 1) % this.formatters.length; - this._formatter = this.formatters[this.formatterIndex]; - if (!this._formatter.canFormatColor(this._color)) { + if (!this.formatter.canFormatColor(this._color)) { return this.selectNextColorFormat(); } - this._onDidChangeFormatter.fire(this._formatter); + this._onDidChangeFormatter.fire(this.formatter); } } diff --git a/src/vs/editor/contrib/hover/browser/modesContentHover.ts b/src/vs/editor/contrib/hover/browser/modesContentHover.ts index f20b53e46da..6fc4a4b59b0 100644 --- a/src/vs/editor/contrib/hover/browser/modesContentHover.ts +++ b/src/vs/editor/contrib/hover/browser/modesContentHover.ts @@ -35,7 +35,6 @@ class ColorHover { constructor( public readonly range: IRange, public readonly color: IColor, - public readonly format: IColorFormat, public readonly availableFormats: IColorFormat[] ) { } } @@ -106,8 +105,8 @@ class ModesContentComputer implements IHoverComputer { if (!didFindColor && colorRange) { didFindColor = true; - const { color, format, availableFormats } = colorRange; - return new ColorHover(d.range, color, format, availableFormats); + const { color, availableFormats } = colorRange; + return new ColorHover(d.range, color, availableFormats); } else { if (!hasHoverContent(d.options.hoverMessage)) { return null; @@ -345,12 +344,23 @@ export class ModesContentHoverWidget extends ContentHoverWidget { fragment.appendChild($('div.hover-row', null, renderedContents)); }); } else { - const formatter = createColorFormatter(msg.format); - const availableFormatters = msg.availableFormats.map(format => createColorFormatter(format)); const { red, green, blue, alpha } = msg.color; const rgba = new RGBA(red * 255, green * 255, blue * 255, alpha * 255); const color = new Color(rgba); - const model = new ColorPickerModel(color, formatter, availableFormatters); + + const availableFormatters = msg.availableFormats.map(format => createColorFormatter(format)); + const text = this._editor.getModel().getValueInRange(msg.range); + + let formatterIndex = 0; + + for (let i = 0; i < availableFormatters.length; i++) { + if (text === availableFormatters[i].formatColor(color)) { + formatterIndex = i; + break; + } + } + + const model = new ColorPickerModel(color, availableFormatters, formatterIndex); const widget = new ColorPickerWidget(fragment, model, this._editor.getConfiguration().pixelRatio); const editorModel = this._editor.getModel(); diff --git a/src/vs/vscode.proposed.d.ts b/src/vs/vscode.proposed.d.ts index 6b0996fe32a..70979b7cad6 100644 --- a/src/vs/vscode.proposed.d.ts +++ b/src/vs/vscode.proposed.d.ts @@ -192,12 +192,6 @@ declare module 'vscode' { */ color: Color; - /** - * The format in which this color is currently formatted. - */ - // TODO can we remove this? - format: ColorFormat; - /** * The other formats this color range supports the color to be formatted in. */ @@ -211,7 +205,7 @@ declare module 'vscode' { * @param format The format in which this color is currently formatted. * @param availableFormats The other formats this color range supports the color to be formatted in. */ - constructor(range: Range, color: Color, format: ColorFormat, availableFormats: ColorFormat[]); + constructor(range: Range, color: Color, availableFormats: ColorFormat[]); } /** diff --git a/src/vs/workbench/api/electron-browser/mainThreadLanguageFeatures.ts b/src/vs/workbench/api/electron-browser/mainThreadLanguageFeatures.ts index 75d929cd5f8..4f6f2e654ad 100644 --- a/src/vs/workbench/api/electron-browser/mainThreadLanguageFeatures.ts +++ b/src/vs/workbench/api/electron-browser/mainThreadLanguageFeatures.ts @@ -278,8 +278,8 @@ export class MainThreadLanguageFeatures extends MainThreadLanguageFeaturesShape return wireCancellationToken(token, proxy.$provideDocumentColors(handle, model.uri)) .then((colorInfos) => { return colorInfos.map(c => { - const format = colorFormatsMap.get(c.format); let availableFormats: modes.IColorFormat[] = []; + c.availableFormats.forEach(f => { availableFormats.push(colorFormatsMap.get(f)); }); @@ -294,7 +294,6 @@ export class MainThreadLanguageFeatures extends MainThreadLanguageFeaturesShape return { color, - format: format, availableFormats: availableFormats, range: c.range }; diff --git a/src/vs/workbench/api/node/extHost.protocol.ts b/src/vs/workbench/api/node/extHost.protocol.ts index ac13cb2ae93..8aa746acb52 100644 --- a/src/vs/workbench/api/node/extHost.protocol.ts +++ b/src/vs/workbench/api/node/extHost.protocol.ts @@ -467,7 +467,6 @@ export abstract class ExtHostHeapServiceShape { } export interface IRawColorInfo { color: [number, number, number, number | undefined]; - format: number; availableFormats: number[]; range: IRange; } diff --git a/src/vs/workbench/api/node/extHostLanguageFeatures.ts b/src/vs/workbench/api/node/extHostLanguageFeatures.ts index 0f4aaac1d93..b18780369fd 100644 --- a/src/vs/workbench/api/node/extHostLanguageFeatures.ts +++ b/src/vs/workbench/api/node/extHostLanguageFeatures.ts @@ -706,12 +706,10 @@ class ColorProviderAdapter { if (Array.isArray(colors)) { const colorInfos: IRawColorInfo[] = []; colors.forEach(ci => { - const format = getCachedId(ci.format); const availableFormats = ci.availableFormats.map(f => getCachedId(f)); colorInfos.push({ color: [ci.color.red, ci.color.green, ci.color.blue, ci.color.alpha], - format: format, availableFormats: availableFormats, range: TypeConverters.fromRange(ci.range) }); diff --git a/src/vs/workbench/api/node/extHostTypes.ts b/src/vs/workbench/api/node/extHostTypes.ts index c41e313f573..a78eff938d8 100644 --- a/src/vs/workbench/api/node/extHostTypes.ts +++ b/src/vs/workbench/api/node/extHostTypes.ts @@ -1045,17 +1045,12 @@ export class ColorRange { color: Color; - format: IColorFormat; - availableFormats: IColorFormat[]; - constructor(range: Range, color: Color, format: IColorFormat, availableFormats: IColorFormat[]) { + constructor(range: Range, color: Color, availableFormats: IColorFormat[]) { if (color && !(color instanceof Color)) { throw illegalArgument('color'); } - if (format && (typeof format !== 'string') && !format.opaque && !format.transparent && typeof format.opaque !== 'string' && typeof format.transparent !== 'string') { - throw illegalArgument('format'); - } if (availableFormats && !Array.isArray(availableFormats)) { throw illegalArgument('availableFormats'); } @@ -1064,7 +1059,6 @@ export class ColorRange { } this.range = range; this.color = color; - this.format = format; this.availableFormats = availableFormats; } }