Skip to content

Latest commit

 

History

History
356 lines (262 loc) · 7.52 KB

README_EN.md

File metadata and controls

356 lines (262 loc) · 7.52 KB

中文

Color Extension

Travis CI LICENSE NPM

This plugin provides some extension methods for color.

  • isHex

    Test if a Hex color.

  • isRgb

    Test if a Rgb color.

Appendix

Install with npm

$ npm install @jiaminghi/color

Use

import { toHex } from '@jiaminghi/color'

// do something

Quick experience

<!--Debug version-->
<script src="https://unpkg.com/@jiaminghi/color/dist/index.js"></script>
<!--Compression version-->
<script src="https://unpkg.com/@jiaminghi/color/dist/index.min.js"></script>
<script>
  const { darken, lighten } = window.Color
  // do something
</script>

Examples

isHex

/**
 * @description Test if a Hex color
 * @param {string} color A color string maybe Hex
 * @return {boolean} Test result
 */
type isHex = (color: string) => boolean

isHex('#000') // true
isHex('#333333') // true
isHex('Not A Color') // false
isHex('rgb(0,0,0)') // false
isHex('rgba(0,0,0,1)') // false

isRgb

/**
 * @description Test if a Rgb color
 * @param {string} color A color string maybe Rgb
 * @return {boolean} Test result
 */
type isRgb = (color: string) => boolean

isRgb('rgb(0,0,0)') // true
isRgb('RGB(0,0,0)') // true
isRgb('Not A Color') // false
isRgb('#000') // false
isRgb('#000000') // false

isRgba

/**
 * @description Test if a Rgba color
 * @param {string} color A color string maybe Rgba
 * @return {boolean} Test result
 */
type isRgba = (color: string) => boolean

isRgba('rgba(0,0,0,1)') // true
isRgba('rgb(0,0,0)') // false
isRgba('Not A Color') // false
isRgba('#000') // false
isRgba('#000000') // false

isRgbOrRgba

/**
 * @description Test if a Rgb or Rgba color
 * @param {string} color A color string maybe Rgb or Rgba
 * @return {boolean} Test result
 */
type isRgbOrRgba = (color: string) => boolean

isRgbOrRgba('rgb(0,0,0)') // true
isRgbOrRgba('RGB(0,0,0)') // true
isRgbOrRgba('rgba(0,0,0,1)') // true
isRgbOrRgba('#000') // false
isRgbOrRgba('Not A Color') // false

darken

/**
 * @description Deepen color
 * @param {string} color   Hex|Rgb|Rgba color or color keyword
 * @param {number} percent Percent of Deepen (1-100)
 * @return {string} Rgba color (Invalid input will throw an error)
 */
type darken = (color: string, percent: number) => string

const before = '#3080E8'

const after = darken(color, 20)
// after = 'rgba(0,77,181,1)'

lighten

/**
 * @description Brighten color
 * @param {string} color   Hex|Rgb|Rgba color or color keyword
 * @param {number} percent Percent of brighten (1-100)
 * @return {string} Rgba color (Invalid input will throw an error)
 */
type lighten = (color: string, percent: number) => string

const before = '#3080E8'

const after = lighten(color, 20)
// after = 'rgba(99,179,255,1)'

fade

/**
 * @description Adjust color opacity
 * @param {string} color   Hex|Rgb|Rgba color or color keyword
 * @param {number} percent Percent of opacity
 * @return {string} Rgba color (Invalid input will throw an error)
 */
type fade = (color: string, percent: number) => string

const before = '#3080E8'

const after = lighten(color, 20)
// after = 'rgba(48,128,232,0.2)'

toHex

/**
 * @description Convert color to Hex color
 * @param {string} color Hex|Rgb|Rgba color or color keyword
 * @return {string} Hex color (Invalid input will throw an error)
 */
type toHex = (color: string) => string

const before = 'rgb(48,128,232)'

const after = toHex(before)
// after = '#3080e8'

toRgb

/**
 * @description Convert color to Rgb|Rgba color
 * @param {string} color   Hex|Rgb|Rgba color or color keyword
 * @param {number} opacity The opacity of color
 * @return {string} Rgb|Rgba color (Invalid input will throw an error)
 */
type toRgb = (color: string, opacity: number) => string

const before = '#3080E8'

const after1 = toRgb(before)
// after1 = 'rgb(48,128,232)'
const after2 = toRgb(before, 0.2)
// after2 = 'rgba(48,128,232,0.2)'

getOpacity

/**
 * @description Get the opacity of color
 * @param {string} color Hex|Rgb|Rgba color or color keyword
 * @return {number} Color opacity (Invalid input will throw an error)
 */
type getOpacity = (color: string) => number

const color1 = '#3080E8'
const color2 = 'rgba(48,128,232,0.2)'

const opacity1 = getOpacity(color1)
// opacity1 = 1
const opacity2 = getOpacity(color2)
// opacity2 = 0.2

getRgbValue

/**
 * @description Get the Rgb value of the color
 * @param {string} color Hex|Rgb|Rgba color or color keyword
 * @return {RgbValue} Rgb value of the color (Invalid input will throw an error)
 */
type RgbValue = [number, number, number]
type getRgbValue = (color: string) => RgbValue

const color = '#3080E8'

const rgbValue = getRgbValue(color)
// rgbValue = [48, 128, 232]

getRgbaValue

/**
 * @description Get the Rgba value of the color
 * @param {string} color Hex|Rgb|Rgba color or color keyword
 * @return {RgbaValue} Rgba value of the color (Invalid input will throw an error)
 */
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)
// rgbaValue1 = [48, 128, 232, 1]
const rgbaValue2 = getRgbaValue(color2)
// rgbaValue2 = [48, 128, 232, 0.2]

getColorFromRgbValue

/**
 * @description Get Color from Rgb|Rgba value
 * @param {Value} value Rgb|Rgba color value
 * @return {string} Rgb|Rgba color (Invalid input will throw an error)
 */
type RgbValue = [number, number, number]
type RgbaValue = [number, number, number, number]
type Value = RgbValue | RgbaValue
type getColorFromRgbValue = (value: Value) => string | null

const value1 = [48, 128, 232]
const value2 = [48, 128, 232, 0.2]

const color1 = getColorFromRgbValue(value1)
// color1 = 'rgb(48,128,232)'
const color2 = getColorFromRgbValue(value2)
// color2 = 'rgba(48,128,232,0.2)'

Color Keywords