color — Color Config
17:43Description
$u.color is a reactive theme color object holding the main, pressed, disabled and light variants of each semantic color, plus text, background, border and other tokens. hexToRgba and mixColor handle color math, while isThemeColor and getThemeColor check and resolve preset theme names.
Examples
The six palette sections reuse the same codes.palette source; only the fields read at runtime differ.
Primary
Lists the four primary shades primary, primaryDisabled, primaryDark and primaryLight, with a swatch showing each value.
View source
<template>
<ux-cell-group :border="false">
<ux-cell-item
v-for="(item, index) in primaryColors"
:key="index"
:title="item.name"
:value="item.value"
:arrow="false"
:border-bottom="!item.last"
>
<template #icon>
<view class="demo-swatch" :style="{ backgroundColor: item.value }"></view>
</template>
</ux-cell-item>
</ux-cell-group>
</template>
<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';
type ColorRow = {
name: string;
value: string;
last: boolean;
};
// 同一结构复用于 primary / success / warning / error / info / text 六组
const primaryColors: ColorRow[] = [
{ name: 'primary', value: $u.color.primary ?? '', last: false },
{ name: 'primaryDisabled', value: $u.color.primaryDisabled ?? '', last: false },
{ name: 'primaryDark', value: $u.color.primaryDark ?? '', last: false },
{ name: 'primaryLight', value: $u.color.primaryLight ?? '', last: true }
];
</script>
<style lang="scss">
/* 色块需真实渲染色值,属必要例外:仅 background-color 走内联 */
.demo-swatch {
width: 20px;
height: 20px;
border-radius: 4px;
border: 1px solid $u-border-color;
}
</style>Success
The same palette structure uses the success, successDisabled, successDark and successLight fields.
View source
<template>
<ux-cell-group :border="false">
<ux-cell-item
v-for="(item, index) in primaryColors"
:key="index"
:title="item.name"
:value="item.value"
:arrow="false"
:border-bottom="!item.last"
>
<template #icon>
<view class="demo-swatch" :style="{ backgroundColor: item.value }"></view>
</template>
</ux-cell-item>
</ux-cell-group>
</template>
<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';
type ColorRow = {
name: string;
value: string;
last: boolean;
};
// 同一结构复用于 primary / success / warning / error / info / text 六组
const primaryColors: ColorRow[] = [
{ name: 'primary', value: $u.color.primary ?? '', last: false },
{ name: 'primaryDisabled', value: $u.color.primaryDisabled ?? '', last: false },
{ name: 'primaryDark', value: $u.color.primaryDark ?? '', last: false },
{ name: 'primaryLight', value: $u.color.primaryLight ?? '', last: true }
];
</script>
<style lang="scss">
/* 色块需真实渲染色值,属必要例外:仅 background-color 走内联 */
.demo-swatch {
width: 20px;
height: 20px;
border-radius: 4px;
border: 1px solid $u-border-color;
}
</style>Warning
The same palette structure uses the warning, warningDisabled, warningDark and warningLight fields.
View source
<template>
<ux-cell-group :border="false">
<ux-cell-item
v-for="(item, index) in primaryColors"
:key="index"
:title="item.name"
:value="item.value"
:arrow="false"
:border-bottom="!item.last"
>
<template #icon>
<view class="demo-swatch" :style="{ backgroundColor: item.value }"></view>
</template>
</ux-cell-item>
</ux-cell-group>
</template>
<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';
type ColorRow = {
name: string;
value: string;
last: boolean;
};
// 同一结构复用于 primary / success / warning / error / info / text 六组
const primaryColors: ColorRow[] = [
{ name: 'primary', value: $u.color.primary ?? '', last: false },
{ name: 'primaryDisabled', value: $u.color.primaryDisabled ?? '', last: false },
{ name: 'primaryDark', value: $u.color.primaryDark ?? '', last: false },
{ name: 'primaryLight', value: $u.color.primaryLight ?? '', last: true }
];
</script>
<style lang="scss">
/* 色块需真实渲染色值,属必要例外:仅 background-color 走内联 */
.demo-swatch {
width: 20px;
height: 20px;
border-radius: 4px;
border: 1px solid $u-border-color;
}
</style>Error
The same palette structure uses the error, errorDisabled, errorDark and errorLight fields.
View source
<template>
<ux-cell-group :border="false">
<ux-cell-item
v-for="(item, index) in primaryColors"
:key="index"
:title="item.name"
:value="item.value"
:arrow="false"
:border-bottom="!item.last"
>
<template #icon>
<view class="demo-swatch" :style="{ backgroundColor: item.value }"></view>
</template>
</ux-cell-item>
</ux-cell-group>
</template>
<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';
type ColorRow = {
name: string;
value: string;
last: boolean;
};
// 同一结构复用于 primary / success / warning / error / info / text 六组
const primaryColors: ColorRow[] = [
{ name: 'primary', value: $u.color.primary ?? '', last: false },
{ name: 'primaryDisabled', value: $u.color.primaryDisabled ?? '', last: false },
{ name: 'primaryDark', value: $u.color.primaryDark ?? '', last: false },
{ name: 'primaryLight', value: $u.color.primaryLight ?? '', last: true }
];
</script>
<style lang="scss">
/* 色块需真实渲染色值,属必要例外:仅 background-color 走内联 */
.demo-swatch {
width: 20px;
height: 20px;
border-radius: 4px;
border: 1px solid $u-border-color;
}
</style>Info
The same palette structure uses the info, infoDisabled, infoDark and infoLight fields.
View source
<template>
<ux-cell-group :border="false">
<ux-cell-item
v-for="(item, index) in primaryColors"
:key="index"
:title="item.name"
:value="item.value"
:arrow="false"
:border-bottom="!item.last"
>
<template #icon>
<view class="demo-swatch" :style="{ backgroundColor: item.value }"></view>
</template>
</ux-cell-item>
</ux-cell-group>
</template>
<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';
type ColorRow = {
name: string;
value: string;
last: boolean;
};
// 同一结构复用于 primary / success / warning / error / info / text 六组
const primaryColors: ColorRow[] = [
{ name: 'primary', value: $u.color.primary ?? '', last: false },
{ name: 'primaryDisabled', value: $u.color.primaryDisabled ?? '', last: false },
{ name: 'primaryDark', value: $u.color.primaryDark ?? '', last: false },
{ name: 'primaryLight', value: $u.color.primaryLight ?? '', last: true }
];
</script>
<style lang="scss">
/* 色块需真实渲染色值,属必要例外:仅 background-color 走内联 */
.demo-swatch {
width: 20px;
height: 20px;
border-radius: 4px;
border: 1px solid $u-border-color;
}
</style>Text
Lists the four text levels mainColor, contentColor, tipsColor and lightColor.
View source
<template>
<ux-cell-group :border="false">
<ux-cell-item
v-for="(item, index) in primaryColors"
:key="index"
:title="item.name"
:value="item.value"
:arrow="false"
:border-bottom="!item.last"
>
<template #icon>
<view class="demo-swatch" :style="{ backgroundColor: item.value }"></view>
</template>
</ux-cell-item>
</ux-cell-group>
</template>
<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';
type ColorRow = {
name: string;
value: string;
last: boolean;
};
// 同一结构复用于 primary / success / warning / error / info / text 六组
const primaryColors: ColorRow[] = [
{ name: 'primary', value: $u.color.primary ?? '', last: false },
{ name: 'primaryDisabled', value: $u.color.primaryDisabled ?? '', last: false },
{ name: 'primaryDark', value: $u.color.primaryDark ?? '', last: false },
{ name: 'primaryLight', value: $u.color.primaryLight ?? '', last: true }
];
</script>
<style lang="scss">
/* 色块需真实渲染色值,属必要例外:仅 background-color 走内联 */
.demo-swatch {
width: 20px;
height: 20px;
border-radius: 4px;
border: 1px solid $u-border-color;
}
</style>Color math
hexToRgba accepts 3- and 6-digit hex values and appends an alpha channel, while the ratio of mixColor is the weight of the target color (0-1).
View source
<template>
<ux-cell-group :border="false">
<ux-cell-item
v-for="(item, index) in mixRows"
:key="index"
:title="item.desc"
:value="item.result"
:arrow="false"
:border-bottom="!item.last"
>
<template #icon>
<view class="demo-swatch" :style="{ backgroundColor: item.result }"></view>
</template>
</ux-cell-item>
</ux-cell-group>
</template>
<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';
type MixRow = {
desc: string;
result: string;
last: boolean;
};
const mixRows: MixRow[] = [
// 十六进制转 rgba:用于主题色半透明底色
{ desc: "$u.hexToRgba('#2979ff', 0.3)", result: $u.hexToRgba('#2979ff', 0.3), last: false },
// 支持 3 位简写
{ desc: "$u.hexToRgba('#fff', 0.5)", result: $u.hexToRgba('#fff', 0.5), last: false },
// 按比例混色:ratio 为 to 的权重(0-1)
{ desc: "$u.mixColor('#2979ff', '#000000', 0.5)", result: $u.mixColor('#2979ff', '#000000', 0.5), last: false },
{ desc: "$u.mixColor('#ffffff', '#000000', 0.1)", result: $u.mixColor('#ffffff', '#000000', 0.1), last: true }
];
</script>Theme color check & lookup
isThemeColor only accepts preset names in the config.type whitelist, while getThemeColor resolves the main / dark / light tone and falls back for non-preset names.
View source
<template>
<ux-cell-group :border="false">
<ux-cell-item
v-for="(item, index) in themeFlagRows"
:key="index"
:title="item.desc"
:arrow="false"
:border-bottom="!item.last"
>
<ux-tag
size="mini"
:type="item.result ? 'success' : 'error'"
:text="item.result ? 'true' : 'false'"
/>
</ux-cell-item>
</ux-cell-group>
<ux-gap :height="12" />
<ux-cell-group :border="false">
<ux-cell-item
v-for="(item, index) in themeColorRows"
:key="index"
:title="item.desc"
:value="item.result"
:arrow="false"
:border-bottom="!item.last"
>
<template #icon>
<view class="demo-swatch" :style="{ backgroundColor: item.result }"></view>
</template>
</ux-cell-item>
</ux-cell-group>
</template>
<script setup lang="ts">
import { $u } from '@/uni_modules/uview-pro-x';
type FlagRow = {
desc: string;
result: boolean;
last: boolean;
};
type ColorRow = {
desc: string;
result: string;
last: boolean;
};
// 预设主题名判定:依赖 config.type 白名单,色值与自定义名均返回 false
const themeFlagRows: FlagRow[] = [
{ desc: "$u.isThemeColor('primary')", result: $u.isThemeColor('primary'), last: false },
{ desc: "$u.isThemeColor('#2979ff')", result: $u.isThemeColor('#2979ff'), last: false },
{ desc: "$u.isThemeColor('brand')", result: $u.isThemeColor('brand'), last: true }
];
// tone 必传:main 主色 / dark 按压态 / light 浅底
const themeColorRows: ColorRow[] = [
{
desc: "$u.getThemeColor('primary', 'main', fallback)",
result: $u.getThemeColor('primary', 'main', $u.strOr($u.color.contentColor)),
last: false
},
{
desc: "$u.getThemeColor('primary', 'dark', fallback)",
result: $u.getThemeColor('primary', 'dark', $u.strOr($u.color.contentColor)),
last: false
},
// 非预设主题名走 fallback
{
desc: "$u.getThemeColor('brand', 'main', '#888888')",
result: $u.getThemeColor('brand', 'main', '#888888'),
last: true
}
];
</script>API
color
$u.color is a reactive<ThemeColor> that is replaced whenever the theme changes.
Returns (object fields):
| Field | Description | Type |
|---|---|---|
primary | Primary color | string | null |
primaryDark | Primary pressed state | string | null |
primaryDisabled | Primary disabled state | string | null |
primaryLight | Primary light background | string | null |
success | Success color | string | null |
successDark | Success pressed state | string | null |
successDisabled | Success disabled state | string | null |
successLight | Success light background | string | null |
warning | Warning color | string | null |
warningDark | Warning pressed state | string | null |
warningDisabled | Warning disabled state | string | null |
warningLight | Warning light background | string | null |
error | Error color | string | null |
errorDark | Error pressed state | string | null |
errorDisabled | Error disabled state | string | null |
errorLight | Error light background | string | null |
info | Info color | string | null |
infoDark | Info pressed state | string | null |
infoDisabled | Info disabled state | string | null |
infoLight | Info light background | string | null |
whiteColor | White | string | null |
blackColor | Black | string | null |
mainColor | Primary text color | string | null |
contentColor | Regular text color | string | null |
tipsColor | Tip text color | string | null |
lightColor | Secondary text color | string | null |
borderColor | Border color | string | null |
dividerColor | Divider color | string | null |
maskColor | Mask color | string | null |
shadowColor | Shadow color | string | null |
bgColor | Page background | string | null |
bgWhite | White background | string | null |
bgGrayLight | Light gray background | string | null |
bgGrayDark | Dark gray background | string | null |
bgBlack | Black background | string | null |
hexToRgba
Parameters:
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| hex | Hex color, #fff and #ffffff supported | string | - | - |
| opacity | Opacity (0-1) | number | 1 | - |
Returns:
| Type | Description |
|---|---|
string | An rgba(r, g, b, opacity) string; invalid or empty input is returned as-is |
mixColor
Parameters:
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| from | Start color, #rgb or #rrggbb | string | null | undefined | - | - |
| to | Target color, #rgb or #rrggbb | string | null | undefined | - | - |
| ratio | Weight of to in the mix (0-1) | number | - | - |
Returns:
| Type | Description |
|---|---|
string | The mixed #rrggbb; when any input is invalid or empty it falls back to to, then to #2979ff |
isThemeColor
Parameters:
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| type | Theme name to check | string | null | - | - |
Returns:
| Type | Description |
|---|---|
boolean | Whether the name is one of the preset themes in config.type |
getThemeColor
Parameters:
| Parameter | Description | Type | Default | Version |
|---|---|---|---|---|
| type | Theme name (primary / success / error / warning / info) | string | - | - |
| tone | Which tone to resolve: main, dark (pressed) or light | 'main' | 'dark' | 'light' | - | - |
| fallback | Fallback for non-preset themes or an empty tone | string | null | - | - |
Returns:
| Type | Description |
|---|---|
string | The resolved theme color; returns fallback when nothing matches |
Notes
- Every
$u.colorfield is typedstring | null, so use?? ''or$u.strOras a fallback when reading colors. isThemeColorrelies on theconfig.typewhitelist, so hex values and custom theme names both returnfalse.mixColortreatsratioas the weight ofto:0yieldsfrom,1yieldsto; results are rounded and clamped to 0-255.hexToRgbaonly accepts 3-digit or 6-digit hex values and returns other input as-is;opacityis not clamped.