-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…0364) When the user apply a background color manually from the UI, this commit adapt the current colors to have a better contrast with the chosen background color irrespective of the used dark/light theme
- Loading branch information
Showing
7 changed files
with
220 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...re_plugins/vis_type_timeseries/public/visualizations/views/timeseries/utils/theme.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { getTheme } from './theme'; | ||
import { LIGHT_THEME, DARK_THEME } from '@elastic/charts'; | ||
|
||
describe('TSVB theme', () => { | ||
it('should return the basic themes if no bg color is specified', () => { | ||
// use original dark/light theme | ||
expect(getTheme(false)).toEqual(LIGHT_THEME); | ||
expect(getTheme(true)).toEqual(DARK_THEME); | ||
|
||
// discard any wrong/missing bg color | ||
expect(getTheme(true, null)).toEqual(DARK_THEME); | ||
expect(getTheme(true, '')).toEqual(DARK_THEME); | ||
expect(getTheme(true, undefined)).toEqual(DARK_THEME); | ||
}); | ||
it('should return a highcontrast color theme for a different background', () => { | ||
// red use a near full-black color | ||
expect(getTheme(false, 'red').axes.axisTitleStyle.fill).toEqual('rgb(23,23,23)'); | ||
|
||
// violet increased the text color to full white for higer contrast | ||
expect(getTheme(false, '#ba26ff').axes.axisTitleStyle.fill).toEqual('rgb(255,255,255)'); | ||
|
||
// light yellow, prefer the LIGHT_THEME fill color because already with a good contrast | ||
expect(getTheme(false, '#fff49f').axes.axisTitleStyle.fill).toEqual('#333'); | ||
}); | ||
}); |
139 changes: 139 additions & 0 deletions
139
...cy/core_plugins/vis_type_timeseries/public/visualizations/views/timeseries/utils/theme.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import colorJS from 'color'; | ||
import { Theme, LIGHT_THEME, DARK_THEME } from '@elastic/charts'; | ||
|
||
function computeRelativeLuminosity(rgb: string) { | ||
return colorJS(rgb).luminosity(); | ||
} | ||
|
||
function computeContrast(rgb1: string, rgb2: string) { | ||
return colorJS(rgb1).contrast(colorJS(rgb2)); | ||
} | ||
|
||
function getAAARelativeLum(bgColor: string, fgColor: string, ratio = 7) { | ||
const relLum1 = computeRelativeLuminosity(bgColor); | ||
const relLum2 = computeRelativeLuminosity(fgColor); | ||
if (relLum1 > relLum2) { | ||
// relLum1 is brighter, relLum2 is darker | ||
return (relLum1 + 0.05 - ratio * 0.05) / ratio; | ||
} else { | ||
// relLum1 is darker, relLum2 is brighter | ||
return Math.min(ratio * (relLum1 + 0.05) - 0.05, 1); | ||
} | ||
} | ||
|
||
function getGrayFromRelLum(relLum: number) { | ||
if (relLum <= 0.0031308) { | ||
return relLum * 12.92; | ||
} else { | ||
return (1.0 + 0.055) * Math.pow(relLum, 1.0 / 2.4) - 0.055; | ||
} | ||
} | ||
|
||
function getGrayRGBfromGray(gray: number) { | ||
const g = Math.round(gray * 255); | ||
return `rgb(${g},${g},${g})`; | ||
} | ||
|
||
function getAAAGray(bgColor: string, fgColor: string, ratio = 7) { | ||
const relLum = getAAARelativeLum(bgColor, fgColor, ratio); | ||
const gray = getGrayFromRelLum(relLum); | ||
return getGrayRGBfromGray(gray); | ||
} | ||
|
||
function findBestContrastColor( | ||
bgColor: string, | ||
lightFgColor: string, | ||
darkFgColor: string, | ||
ratio = 4.5 | ||
) { | ||
const lc = computeContrast(bgColor, lightFgColor); | ||
const dc = computeContrast(bgColor, darkFgColor); | ||
if (lc >= dc) { | ||
if (lc >= ratio) { | ||
return lightFgColor; | ||
} | ||
return getAAAGray(bgColor, lightFgColor, ratio); | ||
} | ||
if (dc >= ratio) { | ||
return darkFgColor; | ||
} | ||
return getAAAGray(bgColor, darkFgColor, ratio); | ||
} | ||
|
||
function isValidColor(color: string | null | undefined): color is string { | ||
if (typeof color !== 'string') { | ||
return false; | ||
} | ||
if (color.length === 0) { | ||
return false; | ||
} | ||
try { | ||
colorJS(color); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
export function getTheme(darkMode: boolean, bgColor?: string | null): Theme { | ||
if (!isValidColor(bgColor)) { | ||
return darkMode ? DARK_THEME : LIGHT_THEME; | ||
} | ||
|
||
const bgLuminosity = computeRelativeLuminosity(bgColor); | ||
const mainTheme = bgLuminosity <= 0.179 ? DARK_THEME : LIGHT_THEME; | ||
const color = findBestContrastColor( | ||
bgColor, | ||
LIGHT_THEME.axes.axisTitleStyle.fill, | ||
DARK_THEME.axes.axisTitleStyle.fill | ||
); | ||
return { | ||
...mainTheme, | ||
axes: { | ||
...mainTheme.axes, | ||
axisTitleStyle: { | ||
...mainTheme.axes.axisTitleStyle, | ||
fill: color, | ||
}, | ||
tickLabelStyle: { | ||
...mainTheme.axes.tickLabelStyle, | ||
fill: color, | ||
}, | ||
axisLineStyle: { | ||
...mainTheme.axes.axisLineStyle, | ||
stroke: color, | ||
}, | ||
tickLineStyle: { | ||
...mainTheme.axes.tickLineStyle, | ||
stroke: color, | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
export function getChartClasses(bgColor?: string) { | ||
// keep the original theme color if no bg color is specified | ||
if (typeof bgColor !== 'string') { | ||
return; | ||
} | ||
const bgLuminosity = computeRelativeLuminosity(bgColor); | ||
return bgLuminosity <= 0.179 ? 'tvbVisTimeSeriesDark' : 'tvbVisTimeSeriesLight'; | ||
} |