Skip to content

Commit

Permalink
Merge branch 'pu/je/felamimail_darkmode_better_background' into '2024…
Browse files Browse the repository at this point in the history
….11'

tweak(Felamimail/Darkmode): Improve email background color detection

See merge request tine20/tine20!4762
  • Loading branch information
jevers committed Jan 16, 2024
2 parents 1f156c9 + dd091c4 commit 410442b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 29 deletions.
6 changes: 6 additions & 0 deletions tine20/Felamimail/js/ComposeEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ Tine.Felamimail.ComposeEditor = Ext.extend(Ext.form.HtmlEditor, {
+ ' filter: invert(1) hue-rotate(180deg); '
+ ' color: #f1f1f1; '
+ '} '
+ 'body.dark-mode a {'
+ ' color: #6c9ad9'
+ '} '
+ 'body.dark-mode a:visited {'
+ ' color: #3c78c9'
+ '}'
+ '</style>'
+ '</head>'
+ '<body style="padding: 5px 0px 0px 5px; margin: 0px" class="' + String(Ext.getBody().dom.classList.value).trim() + '">'
Expand Down
2 changes: 1 addition & 1 deletion tine20/Felamimail/js/MailDetailsPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ Ext.extend(Tine.Felamimail.MailDetailsPanel, Ext.Panel, {
Tine.Tinebase.common.linkifyText(Ext.util.Format.wrapEmojis(body), function(linkified) {
const bodyEl = this.getMessageRecordPanel().getEl().query('div[class=preview-panel-felamimail-body]')[0];
bodyEl.innerHTML = Ext.util.Format.linkSaveHtmlEncodeStepTwo(linkified);
contrastColors.adjustColors(bodyEl);
contrastColors.findBackground(bodyEl);
Ext.fly(bodyEl).update(bodyEl.innerHTML);
}, this.panel);
}
Expand Down
60 changes: 38 additions & 22 deletions tine20/Tinebase/js/util/contrastColors.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,58 @@
*/

const contrastColors = {
adjustColors: (element) => {
findBackground: (element, brightnesses = [], isRoot = true) => {
_.forEach(element.children, (c) => {
contrastColors.adjustColors(c)
if (c.classList.contains('felamimail-body-signature-current')) {
return;
}
if (c.classList.contains('felamimail-body-blockquote')) {
// quoted email gets its own background
contrastColors.findBackground(c)
} else {
contrastColors.findBackground(c, brightnesses, false)
}
})

let bgColor = element.style.getPropertyValue('background-color'),
fgColor = element.style.getPropertyValue('color')
fgColor = element.style.getPropertyValue('color')

if (element.tagName === 'FONT' && fgColor === '') {
fgColor = element.getAttribute('color')
fgColor = element.getAttribute('color') || ''
}

if (bgColor === '' && fgColor !== '') {
let realFg = window.getComputedStyle(element).getPropertyValue('color'),
newBg = contrastColors.getContrastColor(realFg)
if (newBg !== '') {
element.style.backgroundColor = newBg
}
return
brightnesses.push(contrastColors.getBrightness(fgColor))
}

if (fgColor === '' && bgColor !== '') {
let realBg = window.getComputedStyle(element).getPropertyValue('background-color'),
newFg = contrastColors.getContrastColor(realBg)
if (newFg !== '') {
element.style.color = newFg
if (isRoot) {
if (brightnesses.length === 0) {
return
}

let count = brightnesses.length
let sum = brightnesses.reduce((a, current) => {
return a + current
}, 0)

let brightness = sum / count;
if (brightness > 128) {
if (contrastColors.getBrightness(getComputedStyle(element).backgroundColor) > 128
|| getComputedStyle(element).backgroundColor === 'rgba(0, 0, 0, 0)')
{
element.style.backgroundColor = '#171717'
element.style.color = '#ffffff'
}
} else {
if (contrastColors.getBrightness(getComputedStyle(element).backgroundColor) <= 128) {
element.style.backgroundColor = '#F3F6F7'
element.style.color = '#171717'
}
}
}
},

getContrastColor: (color) => {
getBrightness: (color) => {
let r, g, b
if (color.startsWith('#')) {
let m = color.substr(1).match(color.length === 7 ? /(\S{2})/g : /(\S{1})/g);
Expand All @@ -49,12 +70,7 @@ const contrastColors = {
g = m[1]
b = m[2]
}
let brightness = ((r * 299) + (g * 587) + (b * 114)) / 1000
if (brightness > 128) {
return '#000000'
} else {
return '#FFFFFF'
}
return ((r * 299) + (g * 587) + (b * 114)) / 1000
}
}

Expand Down
6 changes: 0 additions & 6 deletions tine20/library/ExtJS/src/widgets/form/HtmlEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ new Ext.Panel({
* @param {Object} config
* @xtype htmleditor
*/

import {contrastColors} from "../../../../../Tinebase/js/util/contrastColors";

Ext.form.HtmlEditor = Ext.extend(Ext.form.Field, {
/**
* @cfg {Boolean} enableFormat Enable the bold, italic and underline buttons (defaults to true)
Expand Down Expand Up @@ -701,7 +698,6 @@ Ext.form.HtmlEditor = Ext.extend(Ext.form.Field, {
this.setDesignMode(true);
this.fireEvent('push', this, v);
}

}
},

Expand Down Expand Up @@ -872,8 +868,6 @@ Ext.form.HtmlEditor = Ext.extend(Ext.form.Field, {
this.selectedImage = false
}

contrastColors.adjustColors(e.getTarget())

this.updateToolbar(e);
},

Expand Down

0 comments on commit 410442b

Please sign in to comment.