ENGLISH
Color Extension
这是一个提供如下几个方法的颜色插件。
附录
npm 安装
$ npm install @jiaminghi/color
使用
import { toHex } from '@jiaminghi/color'
快速体验
<script src="https://unpkg.com/@jiaminghi/color/dist/index.js"></script>
<script src="https://unpkg.com/@jiaminghi/color/dist/index.min.js"></script>
<script>
const { darken, lighten } = window.Color
</script>
示例
isHex
type isHex = (color: string) => boolean
isHex('#000')
isHex('#333333')
isHex('Not A Color')
isHex('rgb(0,0,0)')
isHex('rgba(0,0,0,1)')
isRgb
type isRgb = (color: string) => boolean
isRgb('rgb(0,0,0)')
isRgb('RGB(0,0,0)')
isRgb('Not A Color')
isRgb('#000')
isRgb('#000000')
isRgba
type isRgba = (color: string) => boolean
isRgba('rgba(0,0,0,1)')
isRgba('rgb(0,0,0)')
isRgba('Not A Color')
isRgba('#000')
isRgba('#000000')
isRgbOrRgba
type isRgbOrRgba = (color: string) => boolean
isRgbOrRgba('rgb(0,0,0)')
isRgbOrRgba('RGB(0,0,0)')
isRgbOrRgba('rgba(0,0,0,1)')
isRgbOrRgba('#000')
isRgbOrRgba('Not A Color')
darken
type darken = (color: string, percent: number) => string
const before = '#3080E8'
const after = darken(color, 20)
lighten
type lighten = (color: string, percent: number) => string
const before = '#3080E8'
const after = lighten(color, 20)
fade
type fade = (color: string, percent: number) => string
const before = '#3080E8'
const after = lighten(color, 20)
toHex
type toHex = (color: string) => string
const before = 'rgb(48,128,232)'
const after = toHex(before)
toRgb
type toRgb = (color: string, opacity: number) => string
const before = '#3080E8'
const after1 = toRgb(before)
const after2 = toRgb(before, 0.2)
getOpacity
type getOpacity = (color: string) => number
const color1 = '#3080E8'
const color2 = 'rgba(48,128,232,0.2)'
const opacity1 = getOpacity(color1)
const opacity2 = getOpacity(color2)
getRgbValue
type RgbValue = [number, number, number]
type getRgbValue = (color: string) => RgbValue
const color = '#3080E8'
const rgbValue = getRgbValue(color)
getRgbaValue
type RgbaValue = [number, number, number, number]
type getRgbaValue = (color: string) => RgbaValue
const color1 = '#3080E8'
const color2 = 'rgba(48,128,232,0.2)'
const rgbaValue1 = getRgbaValue(color1)
const rgbaValue2 = getRgbaValue(color2)
getColorFromRgbValue
type RgbValue = [number, number, number]
type RgbaValue = [number, number, number, number]
type Value = RgbValue | RgbaValue
type getColorFromRgbValue = (value: Value) => string
const value1 = [48, 128, 232]
const value2 = [48, 128, 232, 0.2]
const color1 = getColorFromRgbValue(value1)
const color2 = getColorFromRgbValue(value2)
颜色关键字