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

@lazy-color/rand-util

v1.0.6

Published

隨機顏色生成工具,提供 RGB 色彩隨機值的工具函數 (Random color generation utilities providing RGB color random value functions)

Downloads

174

Readme

@lazy-color/rand-util

隨機顏色生成工具,提供 RGB 色彩隨機值的工具函數 (Random color generation utilities providing RGB color random value functions)

安裝 (Installation)

# 使用 yarn / Using yarn
yarn add @lazy-color/rand-util

# 使用 yarn-tool / Using yarn-tool
yarn-tool add @lazy-color/rand-util
# yt 是 yarn-tool 的別名 / yt is an alias for yarn-tool
yt add @lazy-color/rand-util

# 使用 pnpm / Using pnpm
pnpm add @lazy-color/rand-util

# 使用 npm / Using npm
npm install @lazy-color/rand-util

使用方式 (Usage)

import { _rgbRand, _rgbObjectRand, _rgbObjectToArray, IOptionsRandColorUtil } from '@lazy-color/rand-util';

// 基本 RGB 陣列隨機生成
const rgb1 = _rgbRand(); // e.g., [123, 45, 67]
const rgb2 = _rgbRand([100, 50], { randFn: () => 0.5 }); // 從 [100, 50, 255] 開始

// 基本 RGB 物件隨機生成
const rgbObj1 = _rgbObjectRand(); // e.g., { r: 123, g: 45, b: 67 }
const rgbObj2 = _rgbObjectRand({ r: 255 }, { randFn: () => 0.5 }); // 從 { r: 255, g: 255, b: 255 } 開始

// RGB 物件轉陣列
const rgbObj = { r: 255, g: 128, b: 0 };
const rgbArray = _rgbObjectToArray(rgbObj); // [255, 128, 0]

// 自定義隨機函式(例如:種子隨機)
const seededRand = _rgbRand(undefined, {
  randFn: () => {
    // 簡單的線性同餘生成器
    let seed = Date.now();
    return () => {
      seed = (seed * 9301 + 49297) % 233280;
      return seed / 233280;
    };
  }
});
const color = _rgbRand(undefined, seededRand);

API 參考

_rgbRand(_rgba?, opts?)

產生隨機的 RGB 陣列 ([r, g, b]),每個值在 0-255 之間。

參數:

  • _rgba (T, 可選): 可選的初始陣列,用於填充缺失的值
  • opts (IOptionsRandColorUtil, 可選): 配置選項

返回值: 隨機 RGB 陣列

_rgbObjectRand(_rgba?, opts?)

產生隨機的 RGB 物件 ({ r, g, b }),每個值在 0-255 之間。

參數:

  • _rgba (T, 可選): 可選的初始物件,用於填充缺失的值
  • opts (IOptionsRandColorUtil, 可選): 配置選項

返回值: 隨機 RGB 物件

_rgbObjectToArray<T extends { r: number, g: number, b: number, a?: number }>(_rgba)

將 RGB 物件轉換為 RGB 陣列格式。

參數:

  • _rgba (T): RGB 物件

返回值: RGB 陣列

IOptionsRandColorUtil 介面

  • randFn (() => number, 可選): 自定義隨機函式,預設為 Math.random

範例

基本使用

import { _rgbRand, _rgbObjectRand } from '@lazy-color/rand-util';

// 產生隨機 RGB 陣列
const rgb = _rgbRand();
console.log(rgb); // [123, 45, 67]

// 產生隨機 RGB 物件
const rgbObj = _rgbObjectRand();
console.log(rgbObj); // { r: 123, g: 45, b: 67 }

指定初始值

import { _rgbRand } from '@lazy-color/rand-util';

// 從部分初始值開始
const rgb = _rgbRand([255, 128]); // [255, 128, 隨機0-255]

自定義隨機函式

import { _rgbRand } from '@lazy-color/rand-util';

// 使用種子隨機產生可重複的結果
const seeded = _rgbRand(undefined, {
  randFn: () => {
    let seed = 12345;
    return () => {
      seed = (seed * 16807) % 2147483647;
      return seed / 2147483647;
    };
  }
});
const color1 = _rgbRand(undefined, seeded);
const color2 = _rgbRand(undefined, seeded); // 相同的序列