Skip to content

Commit

Permalink
feat(dark): enable setting customized dark mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Ovilia committed Aug 19, 2024
1 parent 99c2420 commit 5c86209
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 14 deletions.
16 changes: 10 additions & 6 deletions src/canvas/Painter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Layer, { LayerConfig } from './Layer';
import requestAnimationFrame from '../animation/requestAnimationFrame';
import env from '../core/env';
import Displayable from '../graphic/Displayable';
import { WXCanvasRenderingContext } from '../core/types';
import { Dictionary, WXCanvasRenderingContext } from '../core/types';
import { GradientObject } from '../graphic/Gradient';
import { ImagePatternObject } from '../graphic/Pattern';
import Storage from '../Storage';
Expand Down Expand Up @@ -69,7 +69,8 @@ interface CanvasPainterOption {
width?: number | string // Can be 10 / 10px / auto
height?: number | string,
useDirtyRect?: boolean,
darkMode?: boolean
darkMode?: boolean,
darkColorMap?: Dictionary<string>
}

export default class CanvasPainter implements PainterBase {
Expand Down Expand Up @@ -277,7 +278,8 @@ export default class CanvasPainter implements PainterBase {
inHover: true,
viewWidth: this._width,
viewHeight: this._height,
darkMode: this._opts.darkMode
darkMode: this._opts.darkMode,
darkColorMap: this._opts.darkColorMap
};

let ctx;
Expand Down Expand Up @@ -420,7 +422,8 @@ export default class CanvasPainter implements PainterBase {
prevEl: null,
viewWidth: this._width,
viewHeight: this._height,
darkMode: this._opts.darkMode
darkMode: this._opts.darkMode,
darkColorMap: this._opts.darkColorMap
};

for (i = start; i < layer.__endIndex; i++) {
Expand Down Expand Up @@ -791,7 +794,7 @@ export default class CanvasPainter implements PainterBase {
setBackgroundColor(backgroundColor: string | GradientObject | ImagePatternObject) {
// TODO: fix when is gradient or pattern
this._backgroundColor = this._opts.darkMode
? convertToDark(backgroundColor as string, 'fill')
? convertToDark(backgroundColor as string, 'fill', this._opts.darkColorMap)
: backgroundColor;

util.each(this._layers, layer => {
Expand Down Expand Up @@ -958,7 +961,8 @@ export default class CanvasPainter implements PainterBase {
inHover: false,
viewWidth: this._width,
viewHeight: this._height,
darkMode: this._opts.darkMode
darkMode: this._opts.darkMode,
darkColorMap: this._opts.darkColorMap
};
const displayList = this.storage.getDisplayList(true);
for (let i = 0, len = displayList.length; i < len; i++) {
Expand Down
20 changes: 14 additions & 6 deletions src/canvas/graphic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { GradientObject } from '../graphic/Gradient';
import { ImagePatternObject, InnerImagePatternObject } from '../graphic/Pattern';
import { LinearGradientObject } from '../graphic/LinearGradient';
import { RadialGradientObject } from '../graphic/RadialGradient';
import { ZRCanvasRenderingContext } from '../core/types';
import { Dictionary, ZRCanvasRenderingContext } from '../core/types';
import { createOrUpdateImage, isImageReady } from '../graphic/helper/image';
import { getCanvasGradient, isClipPathChanged } from './helper';
import Path, { PathStyleProps } from '../graphic/Path';
Expand Down Expand Up @@ -441,7 +441,7 @@ function bindPathAndTextCommonStyle(
styleChanged = true;
}
isValidStrokeFillStyle(style.fill) && (ctx.fillStyle = scope.darkMode
? convertToDark(style.fill, 'fill')
? convertToDark(style.fill, 'fill', scope.darkColorMap)
: style.fill
);
}
Expand All @@ -451,7 +451,7 @@ function bindPathAndTextCommonStyle(
styleChanged = true;
}
isValidStrokeFillStyle(style.stroke) && (ctx.strokeStyle = scope.darkMode
? convertToDark(style.stroke, 'stroke')
? convertToDark(style.stroke, 'stroke', scope.darkColorMap)
: style.stroke
);
}
Expand Down Expand Up @@ -575,6 +575,7 @@ export type BrushScope = {
lastDrawType?: number

darkMode?: boolean
darkColorMap?: Dictionary<string>
}

// If path can be batched
Expand Down Expand Up @@ -611,12 +612,18 @@ function getStyle(el: Displayable, inHover?: boolean) {
return inHover ? (el.__hoverStyle || el.style) : el.style;
}

export function brushSingle(ctx: CanvasRenderingContext2D, el: Displayable, darkMode: boolean) {
export function brushSingle(
ctx: CanvasRenderingContext2D,
el: Displayable,
darkMode: boolean,
darkColorMap: Dictionary<string>
) {
const scope: BrushScope = {
inHover: false,
viewWidth: 0,
viewHeight: 0,
darkMode
darkMode,
darkColorMap
};
brush(ctx, el, scope, true);
}
Expand Down Expand Up @@ -801,7 +808,8 @@ function brushIncremental(
viewWidth: scope.viewWidth,
viewHeight: scope.viewHeight,
inHover: scope.inHover,
darkMode: scope.darkMode
darkMode: scope.darkMode,
darkColorMap: scope.darkColorMap
};
let i;
let len;
Expand Down
28 changes: 27 additions & 1 deletion src/tool/color.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import LRU from '../core/LRU';
import { Dictionary } from '../core/types';
import { extend, isGradientObject, isString, map } from '../core/util';
import { GradientObject } from '../graphic/Gradient';

Expand Down Expand Up @@ -604,11 +605,21 @@ export type ColorAttributeType = 'fill' | 'stroke' | 'textFill';
* @param lightColor color in light mode
* @return color in dark mode, in rgba format
*/
export function convertToDark(lightColor: string, type: ColorAttributeType): string {
export function convertToDark(
lightColor: string,
type: ColorAttributeType,
darkColorMap: Dictionary<string>
): string {
let colorArr = parse(lightColor);

if (colorArr) {
const colorStr = stringify(colorArr, 'rgba');
if (darkColorMap && darkColorMap[colorStr]) {
return darkColorMap[colorStr];
}

colorArr = rgba2hsla(colorArr);

switch (type) {
// TODO: Probably using other color space to enhance the result.
// Just a quick demo for now.
Expand All @@ -629,3 +640,18 @@ export function convertToDark(lightColor: string, type: ColorAttributeType): str
return stringify(hsla2rgba(colorArr), 'rgba');
}
}

export function normalizeColorMap(map: Dictionary<string>): Dictionary<string> {
if (!map) {
return {};
}

const normalizedMap: Dictionary<string> = {};
for (let key in map) {

Check failure on line 650 in src/tool/color.ts

View workflow job for this annotation

GitHub Actions / lint (18.x)

The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype
const normalizedKey = stringify(parse(key), 'rgba');
if (normalizedKey) {
normalizedMap[normalizedKey] = stringify(parse(map[key]), 'rgba');
}
}
return normalizedMap;
}
6 changes: 5 additions & 1 deletion src/zrender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { GradientObject } from './graphic/Gradient';
import { PatternObject } from './graphic/Pattern';
import { EventCallback } from './core/Eventful';
import Displayable from './graphic/Displayable';
import { lum } from './tool/color';
import { lum, normalizeColorMap } from './tool/color';
import { DARK_MODE_THRESHOLD } from './config';
import Group from './graphic/Group';

Expand Down Expand Up @@ -123,6 +123,9 @@ class ZRender {
? false
: (opts.darkMode === 'dark' ? true : isDark);

opts.darkColorMap = normalizeColorMap(opts.darkColorMap);
console.log(opts.darkColorMap)

const painter = new painterCtors[rendererType](dom, storage,
{
darkMode,
Expand Down Expand Up @@ -502,6 +505,7 @@ export interface ZRenderInitOpt {
width?: number | string // 10, 10px, 'auto'
height?: number | string
darkMode?: 'auto' | 'light' | 'dark'
darkColorMap?: Dictionary<string>,
useDirtyRect?: boolean
useCoarsePointer?: 'auto' | boolean
pointerSize?: number
Expand Down

0 comments on commit 5c86209

Please sign in to comment.