@@ -11,6 +11,7 @@ import { Event, Emitter } from 'vs/base/common/event';
1111import * as nls from 'vs/nls' ;
1212import { Extensions as JSONExtensions , IJSONContributionRegistry } from 'vs/platform/jsonschemas/common/jsonContributionRegistry' ;
1313import { RunOnceScheduler } from 'vs/base/common/async' ;
14+ import { assertNever } from 'vs/base/common/types' ;
1415
1516// ------ API types
1617
@@ -24,11 +25,21 @@ export interface ColorContribution {
2425 readonly deprecationMessage : string | undefined ;
2526}
2627
27-
28- export interface ColorFunction {
29- ( theme : IColorTheme ) : Color | undefined ;
28+ export const enum ColorTransformType {
29+ Darken ,
30+ Lighten ,
31+ Transparent ,
32+ OneOf ,
33+ LessProminent ,
3034}
3135
36+ export type ColorTransform =
37+ | { op : ColorTransformType . Darken ; value : ColorValue ; factor : number }
38+ | { op : ColorTransformType . Lighten ; value : ColorValue ; factor : number }
39+ | { op : ColorTransformType . Transparent ; value : ColorValue ; factor : number }
40+ | { op : ColorTransformType . OneOf ; values : readonly ColorValue [ ] }
41+ | { op : ColorTransformType . LessProminent ; value : ColorValue ; background : ColorValue ; factor : number ; transparency : number } ;
42+
3243export interface ColorDefaults {
3344 light : ColorValue | null ;
3445 dark : ColorValue | null ;
@@ -38,7 +49,7 @@ export interface ColorDefaults {
3849/**
3950 * A Color Value is either a color literal, a reference to an other color or a derived color
4051 */
41- export type ColorValue = Color | string | ColorIdentifier | ColorFunction ;
52+ export type ColorValue = Color | string | ColorIdentifier | ColorTransform ;
4253
4354// color registry
4455export const Extensions = {
@@ -495,63 +506,63 @@ export const chartsPurple = registerColor('charts.purple', { dark: '#B180D7', li
495506
496507// ----- color functions
497508
498- export function darken ( colorValue : ColorValue , factor : number ) : ColorFunction {
499- return ( theme ) => {
500- let color = resolveColorValue ( colorValue , theme ) ;
501- if ( color ) {
502- return color . darken ( factor ) ;
503- }
504- return undefined ;
505- } ;
509+ export function executeTransform ( transform : ColorTransform , theme : IColorTheme ) {
510+ switch ( transform . op ) {
511+ case ColorTransformType . Darken :
512+ return resolveColorValue ( transform . value , theme ) ?. darken ( transform . factor ) ;
513+
514+ case ColorTransformType . Lighten :
515+ return resolveColorValue ( transform . value , theme ) ?. lighten ( transform . factor ) ;
516+
517+ case ColorTransformType . Transparent :
518+ return resolveColorValue ( transform . value , theme ) ?. transparent ( transform . factor ) ;
519+
520+ case ColorTransformType . OneOf :
521+ for ( const candidate of transform . values ) {
522+ const color = resolveColorValue ( candidate , theme ) ;
523+ if ( color ) {
524+ return color ;
525+ }
526+ }
527+ return undefined ;
528+
529+ case ColorTransformType . LessProminent :
530+ const from = resolveColorValue ( transform . value , theme ) ;
531+ if ( ! from ) {
532+ return undefined ;
533+ }
534+
535+ const backgroundColor = resolveColorValue ( transform . background , theme ) ;
536+ if ( ! backgroundColor ) {
537+ return from . transparent ( transform . factor * transform . transparency ) ;
538+ }
539+
540+ return from . isDarkerThan ( backgroundColor )
541+ ? Color . getLighterColor ( from , backgroundColor , transform . factor ) . transparent ( transform . transparency )
542+ : Color . getDarkerColor ( from , backgroundColor , transform . factor ) . transparent ( transform . transparency ) ;
543+ default :
544+ throw assertNever ( transform ) ;
545+ }
506546}
507547
508- export function lighten ( colorValue : ColorValue , factor : number ) : ColorFunction {
509- return ( theme ) => {
510- let color = resolveColorValue ( colorValue , theme ) ;
511- if ( color ) {
512- return color . lighten ( factor ) ;
513- }
514- return undefined ;
515- } ;
548+ export function darken ( colorValue : ColorValue , factor : number ) : ColorTransform {
549+ return { op : ColorTransformType . Darken , value : colorValue , factor } ;
516550}
517551
518- export function transparent ( colorValue : ColorValue , factor : number ) : ColorFunction {
519- return ( theme ) => {
520- let color = resolveColorValue ( colorValue , theme ) ;
521- if ( color ) {
522- return color . transparent ( factor ) ;
523- }
524- return undefined ;
525- } ;
552+ export function lighten ( colorValue : ColorValue , factor : number ) : ColorTransform {
553+ return { op : ColorTransformType . Lighten , value : colorValue , factor } ;
526554}
527555
528- export function oneOf ( ...colorValues : ColorValue [ ] ) : ColorFunction {
529- return ( theme ) => {
530- for ( let colorValue of colorValues ) {
531- let color = resolveColorValue ( colorValue , theme ) ;
532- if ( color ) {
533- return color ;
534- }
535- }
536- return undefined ;
537- } ;
556+ export function transparent ( colorValue : ColorValue , factor : number ) : ColorTransform {
557+ return { op : ColorTransformType . Transparent , value : colorValue , factor } ;
538558}
539559
540- function lessProminent ( colorValue : ColorValue , backgroundColorValue : ColorValue , factor : number , transparency : number ) : ColorFunction {
541- return ( theme ) => {
542- let from = resolveColorValue ( colorValue , theme ) ;
543- if ( from ) {
544- let backgroundColor = resolveColorValue ( backgroundColorValue , theme ) ;
545- if ( backgroundColor ) {
546- if ( from . isDarkerThan ( backgroundColor ) ) {
547- return Color . getLighterColor ( from , backgroundColor , factor ) . transparent ( transparency ) ;
548- }
549- return Color . getDarkerColor ( from , backgroundColor , factor ) . transparent ( transparency ) ;
550- }
551- return from . transparent ( factor * transparency ) ;
552- }
553- return undefined ;
554- } ;
560+ export function oneOf ( ...colorValues : ColorValue [ ] ) : ColorTransform {
561+ return { op : ColorTransformType . OneOf , values : colorValues } ;
562+ }
563+
564+ function lessProminent ( colorValue : ColorValue , backgroundColorValue : ColorValue , factor : number , transparency : number ) : ColorTransform {
565+ return { op : ColorTransformType . LessProminent , value : colorValue , background : backgroundColorValue , factor, transparency } ;
555566}
556567
557568// ----- implementation
@@ -569,8 +580,8 @@ export function resolveColorValue(colorValue: ColorValue | null, theme: IColorTh
569580 return theme . getColor ( colorValue ) ;
570581 } else if ( colorValue instanceof Color ) {
571582 return colorValue ;
572- } else if ( typeof colorValue === 'function ' ) {
573- return colorValue ( theme ) ;
583+ } else if ( typeof colorValue === 'object ' ) {
584+ return executeTransform ( colorValue , theme ) ;
574585 }
575586 return undefined ;
576587}
0 commit comments