Utility function to detect background colour and select either light or dark default hometown logo

This commit is contained in:
Emma Winston 2019-09-17 16:57:08 +01:00
parent 87ad6ac03a
commit 2ebe82c49b
3 changed files with 38 additions and 2 deletions

View File

@ -12,8 +12,10 @@ import Motion from '../ui/util/optional_motion';
import spring from 'react-motion/lib/spring'; import spring from 'react-motion/lib/spring';
import SearchResultsContainer from './containers/search_results_container'; import SearchResultsContainer from './containers/search_results_container';
import { changeComposing } from '../../actions/compose'; import { changeComposing } from '../../actions/compose';
import elephantUIPlane from '../../../images/elephant_ui_plane.svg'; import defaultmascotlight from '../../../images/defaultmascotlight.svg';
import defaultmascotdark from '../../../images/defaultmascotdark.svg';
import { mascot } from '../../initial_state'; import { mascot } from '../../initial_state';
import {svgSelect} from './util/svg_select';
import Icon from 'mastodon/components/icon'; import Icon from 'mastodon/components/icon';
const messages = defineMessages({ const messages = defineMessages({
@ -32,6 +34,13 @@ const mapStateToProps = (state, ownProps) => ({
showSearch: ownProps.multiColumn ? state.getIn(['search', 'submitted']) && !state.getIn(['search', 'hidden']) : ownProps.isSearchPage, showSearch: ownProps.multiColumn ? state.getIn(['search', 'submitted']) && !state.getIn(['search', 'hidden']) : ownProps.isSearchPage,
}); });
let instanceMascot;
if (mascot) {
instanceMascot = <img alt='' draggable='false' src={mascot} />;
} else {
instanceMascot = <img alt='' draggable='false' className="defaultmascot" src={svgSelect(defaultmascotlight, defaultmascotdark)} />;
}
export default @connect(mapStateToProps) export default @connect(mapStateToProps)
@injectIntl @injectIntl
class Compose extends React.PureComponent { class Compose extends React.PureComponent {
@ -110,7 +119,7 @@ class Compose extends React.PureComponent {
<ComposeFormContainer /> <ComposeFormContainer />
<div className='drawer__inner__mastodon'> <div className='drawer__inner__mastodon'>
<img alt='' draggable='false' src={mascot || elephantUIPlane} /> {instanceMascot}
</div> </div>
</div>} </div>}

View File

@ -0,0 +1,21 @@
export function svgSelect(light, dark) {
var svgbg = window.getComputedStyle(document.getElementsByClassName("drawer__inner")[0], null).getPropertyValue("background-color");
var rgbArray = ((svgbg.replace(/[^0-9,]/g, "")).split(",")).map(Number).map(x => x/255);
for ( var i = 0; i < rgbArray.length; ++i ) {
if ( rgbArray[i] <= 0.03928 ) {
rgbArray[i] = rgbArray[i] / 12.92
} else {
rgbArray[i] = Math.pow( ( rgbArray[i] + 0.055 ) / 1.055, 2.4);
}
}
var luminance = 0.2126 * rgbArray[0] + 0.7152 * rgbArray[1] + 0.0722 * rgbArray[2];
if ( luminance <= 0.179 ) {
return light;
} else {
return dark;
}
}

View File

@ -2365,6 +2365,12 @@ a.account__display-name {
user-select: none; user-select: none;
} }
img.defaultmascot {
width: 70px;
padding-left: 1rem;
margin-top: -2rem;
}
@media screen and (min-height: 640px) { @media screen and (min-height: 640px) {
display: block; display: block;
} }