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

svg-colorizer

v0.3.1

Published

Modify colors in SVG elements for web and server-side applications.

Readme

SVG colorizer

Dynamically style your SVGs with ease using this versatile utility set. Effortlessly apply a uniform color across an entire SVG or precisely swap out specific colors within its elements. Designed for seamless integration in both browser environments (where they directly manipulate the SVG DOM) and server-side contexts (returning the modified SVG string for your processing needs). These functions detect their execution environments. Beyond color manipulation, this library offers other useful utility functions to streamline your SVG workflows.

Getting started

  1. Install the package with command
npm install svg-colorizer
  1. Import functions in JavaScript/TypeScript file and use them
import { fill, replace, changeBrightness, extractColors, generateRandomColor } from "svg-colorizer";

Examples

Using in the client side

import { fill, replace, generateRandomColor, changeBrightness } from "svg-colorizer";

const svgDOMElement = document.querySelector("svg");

// Generate random colors
const randomColor1 = generateRandomColor();
const randomColor2 = generateRandomColor();

// Fill the entire SVG with one color
fill(svgDOMElement, randomColor1);

// Replace specified colors with other colors
replace(svgDOMElement, [
    {
        target: randomColor1,
        replace: randomColor2
    }
])

// Change brightness
changeBrightness(svgDOMElement, 50);

Using in the server side

import { fill, replace, generateRandomColor, changeBrightness } from "svg-colorizer";

const svgStringElement = "<svg viewBox="0 0 10 10" fill="currentColor"><rect width="10" height="10"/></svg>"

// Generate random colors
const randomColor1 = generateRandomColor();
const randomColor2 = generateRandomColor();

// Fill the entire SVG with one color
const filledString = fill(svgDOMElement, randomColor1);

// Replace specified colors with other colors
const replacedString = replace(filledString, [
    {
        target: randomColor1,
        replace: randomColor2
    }
])

// Change brightness
const changedBrightnessString =  changeBrightness(replacedString, 50);

Available functions

The functions will use either DOM API or string manipulation for achieving their goal, depending whether they are used in client side or server side.

Color modifier functions

Property modifier functions

Extractor functions

Generator functions

Color modifier functions

  (svg: SVGElement | string, 
   color: string,
   ignoreColors?: string[], 
   callback?: () => void
  ) => void | string

Fills the specified SVG element with a given color. Returns string in server side and nothing in client side.

replace

  (svg: SVGElement | string, 
   detailsArray: { target: string, replace: string }[], 
   callback?: () => void
  ) => void | string

Replaces specific colors within the SVG element based on a configuration. Returns string in server side and nothing in client side.

invert

  (svg: SVGElement | string,  
   callback?: () => void
  ) => void | string

Replaces all the colors with their opposite in the color spectre. Returns string in server side and nothing in client side.

Property modifier functions

changeBrightness

  (svg: SVGElement | string, 
   factor: number
  ) => void | string

Changes the brightness of SVG element by replacing all colors in it.Returns string in server side and nothing in client side.

changeAlpha

  (svg: SVGElement | string, 
   factor: number
  ) => void | string

Changes the alpha(opacity) of SVG element by replacing all colors in it.Returns string in server side and nothing in client side.

changeHue

  (svg: SVGElement | string, 
   factor: number
  ) => void | string

Changes the hue of the the SVG element.Returns string in server side and nothing in client side.

changeSaturation

  (svg: SVGElement | string, 
   factor: number
  ) => void | string

Changes the saturation of SVG element by replacing all colors in it.Returns string in server side and nothing in client side.

Extractor functions

extractColors

  (svg: SVGElement | string, 
   onlyParent?: boolean,
   asArray?: boolean
  ) => { 
    fill: string[], 
    stroke: string[],
    stop: string[]
  } | string[]

Extracts the colors used in the SVG element. Returns them as an object or an array depending the asArray option.

Generator functions

generateRandomColor

(format?: "hex" | "rgb") => string

This function generates a random color and returns it in either hexadecimal notation or RGB format depending on the format argument(default is "rgb").