proper color fallbacks

This commit is contained in:
Henry Jameson 2024-10-11 20:48:46 +03:00
parent eeb2dbcb60
commit 1ec1ba8d3e
4 changed files with 39 additions and 7 deletions

View file

@ -130,7 +130,7 @@ export default {
return this.modelValue === 'transparent' return this.modelValue === 'transparent'
}, },
computedColor () { computedColor () {
return this.modelValue && this.modelValue.startsWith('--') return this.modelValue && (this.modelValue.startsWith('--') || this.modelValue.startsWith('$'))
} }
}, },
methods: { methods: {

View file

@ -15,7 +15,7 @@ import Tooltip from 'src/components/tooltip/tooltip.vue'
import ContrastRatio from 'src/components/contrast_ratio/contrast_ratio.vue' import ContrastRatio from 'src/components/contrast_ratio/contrast_ratio.vue'
import Preview from '../theme_tab/theme_preview.vue' import Preview from '../theme_tab/theme_preview.vue'
import { init } from 'src/services/theme_data/theme_data_3.service.js' import { init, findColor } from 'src/services/theme_data/theme_data_3.service.js'
import { import {
getCssRules, getCssRules,
getScopedVersion getScopedVersion
@ -23,7 +23,7 @@ import {
import { serializeShadow, serialize } from 'src/services/theme_data/iss_serializer.js' import { serializeShadow, serialize } from 'src/services/theme_data/iss_serializer.js'
import { parseShadow, deserialize } from 'src/services/theme_data/iss_deserializer.js' import { parseShadow, deserialize } from 'src/services/theme_data/iss_deserializer.js'
import { import {
// rgb2hex, rgb2hex,
hex2rgb, hex2rgb,
getContrastRatio getContrastRatio
} from 'src/services/color_convert/color_convert.js' } from 'src/services/color_convert/color_convert.js'
@ -73,6 +73,7 @@ export default {
Preview Preview
}, },
setup () { setup () {
const exports = {}
// All rules that are made by editor // All rules that are made by editor
const allEditedRules = reactive({}) const allEditedRules = reactive({})
@ -136,7 +137,6 @@ export default {
const selectedPaletteId = ref(0) const selectedPaletteId = ref(0)
const selectedPalette = computed({ const selectedPalette = computed({
get () { get () {
console.log(palettes, toValue(palettes))
return palettes[selectedPaletteId.value] return palettes[selectedPaletteId.value]
}, },
set (newPalette) { set (newPalette) {
@ -327,6 +327,8 @@ export default {
const isLinkColorPresent = isElementPresent('Link', 'textColor', '#000080') const isLinkColorPresent = isElementPresent('Link', 'textColor', '#000080')
const editedIconColor = getEditedElement('Icon', 'textColor') const editedIconColor = getEditedElement('Icon', 'textColor')
const isIconColorPresent = isElementPresent('Icon', 'textColor', '#909090') const isIconColorPresent = isElementPresent('Icon', 'textColor', '#909090')
const editedBorderColor = getEditedElement('Border', 'textColor')
const isBorderColorPresent = isElementPresent('Border', 'textColor', '#909090')
// TODO this is VERY primitive right now, need to make it // TODO this is VERY primitive right now, need to make it
// support variables, fallbacks etc. // support variables, fallbacks etc.
const getContrast = (bg, text) => { const getContrast = (bg, text) => {
@ -353,7 +355,6 @@ export default {
} }
const normalizeShadows = (shadows) => { const normalizeShadows = (shadows) => {
console.log('NORMALIZE')
return shadows?.map(shadow => { return shadows?.map(shadow => {
if (typeof shadow === 'object') { if (typeof shadow === 'object') {
return shadow return shadow
@ -626,6 +627,14 @@ export default {
value: 'foobar' value: 'foobar'
}) })
exports.computeColor = (color) => {
const computedColor = findColor(color, { dynamicVars: {}, staticVars: selectedPalette.value })
if (computedColor) {
return rgb2hex(computedColor)
}
return null
}
const overallPreviewRules = ref() const overallPreviewRules = ref()
const updateOverallPreview = () => { const updateOverallPreview = () => {
try { try {
@ -682,6 +691,7 @@ export default {
author.value = metaIn.author author.value = metaIn.author
website.value = metaIn.website website.value = metaIn.website
onPalettesUpdate(palettesIn.map(x => ({ name: x.variant, ...x.directives })))
console.log('PALETTES', palettesIn) console.log('PALETTES', palettesIn)
Object.keys(allEditedRules).forEach((k) => delete allEditedRules[k]) Object.keys(allEditedRules).forEach((k) => delete allEditedRules[k])
@ -757,6 +767,8 @@ export default {
isLinkColorPresent, isLinkColorPresent,
editedIconColor, editedIconColor,
isIconColorPresent, isIconColorPresent,
editedBorderColor,
isBorderColorPresent,
getContrast, getContrast,
// component shadow // component shadow
@ -788,7 +800,9 @@ export default {
// ## Export and Import // ## Export and Import
exportStyle, exportStyle,
importStyle importStyle,
...exports
} }
} }
} }

View file

@ -165,6 +165,7 @@
> >
<ColorInput <ColorInput
v-model="editedBackgroundColor" v-model="editedBackgroundColor"
:fallback="computeColor(editedBackgroundColor)"
:disabled="!isBackgroundColorPresent" :disabled="!isBackgroundColorPresent"
:label="$t('settings.style.themes3.editor.background')" :label="$t('settings.style.themes3.editor.background')"
/> />
@ -182,6 +183,7 @@
<ColorInput <ColorInput
v-if="componentHas('Text')" v-if="componentHas('Text')"
v-model="editedTextColor" v-model="editedTextColor"
:fallback="computeColor(editedTextColor)"
:label="$t('settings.style.themes3.editor.text_color')" :label="$t('settings.style.themes3.editor.text_color')"
:disabled="!isTextColorPresent" :disabled="!isTextColorPresent"
/> />
@ -230,6 +232,7 @@
<ColorInput <ColorInput
v-if="componentHas('Link')" v-if="componentHas('Link')"
v-model="editedLinkColor" v-model="editedLinkColor"
:fallback="computeColor(editedLinkColor)"
:label="$t('settings.style.themes3.editor.link_color')" :label="$t('settings.style.themes3.editor.link_color')"
:disabled="!isLinkColorPresent" :disabled="!isLinkColorPresent"
/> />
@ -242,6 +245,7 @@
<ColorInput <ColorInput
v-if="componentHas('Icon')" v-if="componentHas('Icon')"
v-model="editedIconColor" v-model="editedIconColor"
:fallback="computeColor(editedIconColor)"
:label="$t('settings.style.themes3.editor.icon_color')" :label="$t('settings.style.themes3.editor.icon_color')"
:disabled="!isIconColorPresent" :disabled="!isIconColorPresent"
/> />
@ -251,6 +255,19 @@
> >
<Checkbox v-model="isIconColorPresent" /> <Checkbox v-model="isIconColorPresent" />
</Tooltip> </Tooltip>
<ColorInput
v-if="componentHas('Border')"
v-model="editedBorderColor"
:fallback="computeColor(editedBorderColor)"
:label="$t('settings.style.themes3.editor.Border_color')"
:disabled="!isBorderColorPresent"
/>
<Tooltip
v-if="componentHas('Border')"
:text="$t('settings.style.themes3.editor.include_in_rule')"
>
<Checkbox v-model="isBorderColorPresent" />
</Tooltip>
</div> </div>
<div <div
key="shadow" key="shadow"
@ -397,6 +414,7 @@
<ColorInput <ColorInput
v-if="selectedVirtualDirectiveValType === 'color'" v-if="selectedVirtualDirectiveValType === 'color'"
v-model="selectedVirtualDirectiveParsed" v-model="selectedVirtualDirectiveParsed"
:fallback="computeColor(selectedVirtualDirectiveParsed)"
:label="$t('settings.style.themes3.editor.variables.virtual_color')" :label="$t('settings.style.themes3.editor.variables.virtual_color')"
/> />
</div> </div>

View file

@ -62,7 +62,7 @@ const findShadow = (shadows, { dynamicVars, staticVars }) => {
}) })
} }
const findColor = (color, { dynamicVars, staticVars }) => { export const findColor = (color, { dynamicVars, staticVars }) => {
if (typeof color !== 'string' || (!color.startsWith('--') && !color.startsWith('$'))) return color if (typeof color !== 'string' || (!color.startsWith('--') && !color.startsWith('$'))) return color
let targetColor = null let targetColor = null
if (color.startsWith('--')) { if (color.startsWith('--')) {