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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@rgba-image/gray

v0.1.0

Published

Work with 1 byte per pixel data

Downloads

5

Readme

gray

Work with 1 byte per pixel data

install

npm install @rgba-image/gray

usage

createGray

Creates an object similar to an ImageData, but the length of the .data property is width * height rather than width * height * 4

const { createGray } = require( '@rgba-image/gray' )

const width = 5
const height = 10
const gray = createGray( width, height )

const data = new Uint8ClampedArray( width, height )

data.fill( 127 )

const grayFromData = createGray( width, height, data )

extractChannel

Extract a single channel from the RGBA data

const { ALPHA_CHANNEL, extractChannel } = require( '@rgba-image/gray' )

// get some image here

const alpha = extractChannel( image, ALPHA_CHANNEL )

const x = 2
const y = 3
const width = 5
const height = 7

const alphaFromRegion = extractChannel( image, ALPHA_CHANNEL, x, y, width, height )

The channels are defined as numbers 0-3:

export const RED_CHANNEL = 0
export const GREEN_CHANNEL = 1
export const BLUE_CHANNEL = 2
export const ALPHA_CHANNEL = 3

There are aliases for extractChannel where you can omit the channel argument:

  • extractRed
  • extractGreen
  • extractBlue
  • extractAlpha

You can also get back an array of channels with extractRgba:

const { extractRgba } = require( '@rgba-image/gray' )

// get some image here

const [ red, green, blue, alpha ] = extractRgba( image )

maskImage

Mask the destination pixels using a GrayImage:

const { maskImage } = require( '@rgba-image/gray' )

// get an image in ImageData format and a mask in `GrayData` format here

maskImage( mask, image )


//mask a region:
const sourceX = 2
const sourceY = 3
const sourceWidth = 5
const sourceHeight = 7
const destX = 4
const destY = 6

maskImage( mask, image, sourceX, sourceY, sourceWith, sourceHeight, destX, destY )

fromAverage

Create a new GrayData image from an ImageData instance, the gray will be the average of the red, green and blue channels, ignoring alpha:

const { fromAverage } = require( '@rgba-image/gray' )

// get an image here

const averaged = fromAverage( image )

// from a region:
const x = 2
const y = 3
const width = 5
const height = 7

const averagedFromRegion = fromRegion( image, x, y, width, height )

fromLightness

Convert an image to grayscale.

Creates a new GrayData image from an ImageData instance, the gray will be the perceived lightness of the red, green and blue channels, ignoring alpha:

const { fromLightness } = require( '@rgba-image/gray' )

// get an image here

const grayscale = fromLightness( image )

// from a region:
const x = 2
const y = 3
const width = 5
const height = 7

const grayscaleFromRegion = fromLightness( image, x, y, width, height )

grayToImage

Create a normal ImageData instance from a GrayData instance

const { grayToImage } = require( '@rgba-image/gray' )

// get a gray image here

const image = grayToImage( gray )

// from a region
const x = 2
const y = 3
const width = 5
const height = 7

const imageFromRegion = grayToImage( gray, x, y, width, height )

// alpha is full opacity by default, you can specify it if you want:

const halfAlphaImage = grayToImage( gray, x, y, width, height, 127 )

combineChannels

Combines four GrayData instances of the same size to create a new ImageData:

const { combineChannels } = require( '@rgba-image/gray' )

// get your gray channels here

const image = combineChannels( red, green, blue, alpha )

By default, grayToImage and combineChannels use create-image

A factory function is exposed so that you can use a custom createImage, such as create-image-browser:

const createImage = require( '@rgba-image/create-image-browser' )
const { GrayToImageFactory } = require( '@rgba-image/gray' )

const { grayToImage, combineChannels } = GrayToImageFactory( createImage )

license

MIT License

Copyright (c) 2018 Nik Coughlin

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.