Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Do not allow transparency in a tab's theme colors, which can prevent …
Browse files Browse the repository at this point in the history
…the theme color rendering the same as in the content frame.

Blend's the theme color with a white background, which is common in web content.

Fix #12803

Also do not throw error when parsing color and no valid value is found
  • Loading branch information
petemill committed Mar 16, 2018
1 parent 6c956b1 commit 9c9180c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
32 changes: 31 additions & 1 deletion js/lib/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
module.exports.parseColor = (color) => {
const div = document.createElement('div')
div.style.color = color
return div.style.color.split('(')[1].split(')')[0].split(',')
const normalizedColor = div.style.color
if (typeof normalizedColor === 'string' &&
normalizedColor.includes('(') &&
normalizedColor.includes(')') &&
normalizedColor.includes(',')) {
return div.style.color.split('(')[1].split(')')[0].split(',')
}
return null
}

module.exports.backgroundRequiresLightText = (color) => {
Expand All @@ -23,3 +30,26 @@ module.exports.backgroundRequiresLightText = (color) => {
module.exports.getTextColorForBackground = (color) => {
return module.exports.backgroundRequiresLightText(color) ? 'black' : 'white'
}

module.exports.removeAlphaChannelForBackground = (color, bR, bG, bB) => {
const rgba = module.exports.parseColor(color)
if (!rgba) {
return null
}
// handle no alpha channel
if (rgba.length !== 4 || Number.isNaN(rgba[3])) {
return color
}

// remove alpha channel, blending color with background
const [oR, oG, oB, oA] = rgba

const newR = blendChannel(oR, bR, oA)
const newG = blendChannel(oG, bG, oA)
const newB = blendChannel(oB, bB, oA)
return `rgb(${newR}, ${newG}, ${newB})`
}

function blendChannel (original, background, alpha) {
return Math.round((original * alpha) + ((1 - alpha) * background))
}
8 changes: 6 additions & 2 deletions js/stores/windowStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const messages = require('../constants/messages')
const debounce = require('../lib/debounce')
const getSetting = require('../settings').getSetting
const UrlUtil = require('../lib/urlutil')
const color = require('../lib/color')
const {l10nErrorText} = require('../../app/common/lib/httpUtil')
const { makeImmutable } = require('../../app/common/state/immutableUtil')
const {aboutUrls, getTargetAboutUrl, newFrameUrl} = require('../lib/appUrlUtil')
Expand Down Expand Up @@ -397,10 +398,13 @@ const doAction = (action) => {
{
const frameKey = action.frameProps.get('key')
if (action.themeColor !== undefined) {
windowState = windowState.setIn(frameStateUtil.frameStatePath(windowState, frameKey).concat(['themeColor']), action.themeColor)
// remove alpha channel
const solidColor = color.removeAlphaChannelForBackground(action.themeColor, 255, 255, 255)
windowState = windowState.setIn(frameStateUtil.frameStatePath(windowState, frameKey).concat(['themeColor']), solidColor)
}
if (action.computedThemeColor !== undefined) {
windowState = windowState.setIn(frameStateUtil.frameStatePath(windowState, frameKey).concat(['computedThemeColor']), action.computedThemeColor)
const solidColor = color.removeAlphaChannelForBackground(action.computedThemeColor, 255, 255, 255)
windowState = windowState.setIn(frameStateUtil.frameStatePath(windowState, frameKey).concat(['computedThemeColor']), solidColor)
}
break
}
Expand Down

0 comments on commit 9c9180c

Please sign in to comment.