pleroma/app/soapbox/reducers/theme.js

46 lines
1.3 KiB
JavaScript
Raw Normal View History

2020-05-30 17:05:01 -07:00
import {
2020-05-30 22:21:41 -07:00
THEME_SET,
THEME_GENERATE,
2020-05-30 17:05:01 -07:00
} from '../actions/theme';
import { Map as ImmutableMap } from 'immutable';
2020-05-30 19:48:37 -07:00
import { brightness, hue, convert } from 'chromatism';
2020-05-30 17:05:01 -07:00
const initialState = ImmutableMap();
2020-05-30 19:48:37 -07:00
const cssrgba = (color, a) => {
const { r, g, b } = convert(color).rgb;
return `rgba(${[r, g, b, a].join(',')})`;
};
2020-05-31 13:17:10 -07:00
const makeContrast = (percent, color, mode) => {
percent = mode === 'light' ? -percent : percent;
return brightness(percent, color);
};
2020-05-30 22:21:41 -07:00
export const generateTheme = (brandColor, mode) => {
2020-05-30 18:11:08 -07:00
return ImmutableMap({
2020-05-30 22:21:41 -07:00
'brand-color': brandColor,
'accent-color': brightness(10, hue(-3, brandColor).hex).hex,
2020-05-30 19:48:37 -07:00
'brand-color-faint': cssrgba(brandColor, 0.1),
2020-05-31 13:17:10 -07:00
'brand-color-med': cssrgba(brandColor, 0.2),
2020-05-31 14:12:41 -07:00
'highlight-text-color': makeContrast(5, brandColor, mode).hex,
2020-05-31 14:27:35 -07:00
'brand-color-hicontrast': makeContrast(15, brandColor, mode).hex,
2020-05-30 22:21:41 -07:00
});
};
export const setTheme = themeData => {
const { 'brand-color': brandColor } = themeData.toObject();
return ImmutableMap(generateTheme(brandColor, 'light')).merge(themeData);
2020-05-30 18:11:08 -07:00
};
2020-05-30 17:05:01 -07:00
export default function theme(state = initialState, action) {
switch(action.type) {
2020-05-30 22:21:41 -07:00
case THEME_GENERATE:
return generateTheme(action.brandColor, action.mode);
case THEME_SET:
return setTheme(ImmutableMap(action.brandColor));
2020-05-30 17:05:01 -07:00
default:
return state;
}
};