npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@tker/color

v1.0.1

Published

颜色处理工具包,提供颜色格式转换、验证和 CSS 变量批量生成功能。

Readme

@tker/color

颜色处理工具包,提供颜色格式转换、验证和 CSS 变量批量生成功能。

安装

pnpm add @tker/color

使用方式

基础使用

import {
  isDarkColor,
  isLightColor,
  convertToHsl,
  convertToHslCssVar,
  convertToRgb,
  isValidColor,
  TinyColor
} from '@tker/color'

// 颜色验证
isValidColor('#ff0000')  // true
isValidColor('red')      // true
isValidColor('invalid')  // false
isValidColor()           // false(无参数)

// 颜色亮度判断
isDarkColor('#000000')   // true
isDarkColor('#333333')   // true
isLightColor('#ffffff')  // true
isLightColor('#eeeeee')  // true

// 格式转换
convertToHsl('#ff0000')            // "hsl(0 100% 50%)"
convertToHslCssVar('#ff0000')      // "0 100% 50%"(用于 CSS hsl() 函数)
convertToRgb('hsl(0 100% 50%)')    // "rgb(255, 0, 0)"

// 使用 TinyColor(重新导出自 @ctrl/tinycolor)
const color = new TinyColor('#ff0000')
color.toHex()        // "ff0000"
color.toRgbString()  // "rgb(255, 0, 0)"

批量生成 CSS 颜色变量

import { generatorColorVariables } from '@tker/color'

const variables = generatorColorVariables([
  { name: 'primary', alias: 'p', color: '#3b82f6' },
  { name: 'success', color: '#22c55e' },
  { name: 'warning', color: '#eab308' },
  { name: 'danger', alias: 'd', color: '#ef4444' }
])

// 返回示例:
// {
//   "--color-primary-50": "hsl(214 91% 95%)",
//   "--color-primary-100": "hsl(214 91% 87%)",
//   "--color-primary-200": "hsl(...)",
//   ...
//   "--color-primary-900": "hsl(214 91% 14%)",
//   "--color-p-50": "hsl(214 91% 95%)",  // alias 生成的同组变量
//   "--p": "214 91% 54%",                // alias 主色值(用于 hsl())
//   ...
// }

生成的变量可直接用于 CSS:

:root {
  /* 使用生成的变量 */
  --color-primary: hsl(var(--p));
  --color-primary-light: hsl(var(--p) / 0.8);
  --bg-primary: hsl(var(--color-primary-100));
}

API

颜色验证

| 函数 | 参数 | 返回值 | 说明 | |------|------|--------|------| | isValidColor(color?) | color?: string | boolean | 验证颜色值是否有效 |

颜色亮度判断

| 函数 | 参数 | 返回值 | 说明 | |------|------|--------|------| | isDarkColor(color) | color: string | boolean | 判断颜色是否为深色 | | isLightColor(color) | color: string | boolean | 判断颜色是否为浅色 |

格式转换

| 函数 | 参数 | 返回值 | 说明 | |------|------|--------|------| | convertToHsl(color) | color: string | string | 转换为 HSL 格式字符串 | | convertToHslCssVar(color) | color: string | string | 转换为 CSS 变量格式(不含 hsl() 包裹) | | convertToRgb(str) | str: string | string | 转换为 RGB 格式字符串 |

CSS 变量生成

| 函数 | 参数 | 返回值 | 说明 | |------|------|--------|------| | generatorColorVariables(colorItems) | ColorItem[] | Record<string, string> | 批量生成 CSS 颜色变量 |

ColorItem 接口

interface ColorItem {
  name: string      // 颜色名称(用于生成变量名)
  alias?: string    // 可选别名(生成简短变量名)
  color: string     // 颜色值(支持 hex、rgb、hsl、颜色名等)
}

重导出

| 导出 | 来源 | 说明 | |------|------|------| | TinyColor | @ctrl/tinycolor | 颜色解析和转换核心类 |

调色板色阶说明

generatorColorVariables 会为每个颜色生成 50-900 共 10 个色阶:

| 色阶 | 用途 | |------|------| | 50 | 最浅色,用于背景高亮 | | 100 | 浅色背景 | | 200 | 次浅色 | | 300 | 中浅色 | | 400 | 中色偏浅 | | 500 | 主色(原始输入颜色) | | 600 | 中色偏深 | | 700 | 中深色 | | 800 | 深色 | | 900 | 最深色,用于深色模式 |

License

MIT