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

react-native-palette

v1.2.2

Published

A react native component to extract prominent colors from an image

Downloads

263

Readme

react-native-palette

A React-Native library which extracts prominent colors from an image. On Android, it wraps the Android Pallete Class. On iOS, it uses code from react-native-color-grabber.

An example app is included.

Getting started

$ npm install react-native-palette --save

or

yarn add react-native-palette

For use on React-Native >= 0.60.0

Use version >= 1.0.0 of this package, which supports autolinking. No further installation steps are required.

For use on React-Native < 0.60.0

Use version 0.4.x of this package and follow linking steps below:

Mostly automatic installation for Android and iOS

$ react-native link react-native-palette

Manual installation

Android

  1. Open up android/app/src/main/java/[...]/MainApplication.java
  • Add import io.palette.RNPalettePackage; to the imports at the top of the file
  • Add new RNPalettePackage() to the list returned by the getPackages() method
  1. Append the following lines to android/settings.gradle:
 include ':react-native-palette'
 project(':react-native-palette').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-palette/android')
  1. Insert the following lines inside the dependencies block in android/app/build.gradle:
   compile project(':react-native-palette')

iOS

  1. Add node_modules/react-native-palette/ios/RNPalette.xcodeproj to your Xcode project

API

getAllSwatches(options, image, (error, swatches) => {})

options

An object containing option properties. These properties are valid for iOS only. On Android, this parameter is ignored.

Property | Type | Info -------- | ---- | ---- threshold | Float | Determines whether white or black text will be selected to contrast with the selected color. It is the value for L, in the complex formula at the end of this StackOverflow comment. The default value is 0.179. quality | String | One of "low", "medium", or "high". Higher quality extracts more colors, takes more time and consumes more memory. Default is "low". alpha | Float | From 0 to 1. Determine alpha of hex property. Default is 1. This value is multiplied by the alpha returned from the underlying platform-specific code. It does not affect the alpha of the color (rgba), titleTextColor, or bodyTextColor fields.

image

A path to an image such as that returned by react-native-image-picker. For iOS use the origURL field of the image picker response for images from assets-library://. For Android use the path field. Also for IOS, you can use a local URI, such as that returned by RNFS.LibraryDirectoryPath in the react-native-fs library.

For versions of react-native-image-picker >= 3.0.0, use response.assets[0].uri.slice(7)

callback

The callback is passed an error parameter and an array of swatches representing the dominant colors in the image. Typically 16 swatches are returned on Android, fewer on iOS.

getNamedSwatches(image, (error, swatches) => {})

Android only.

image

Same as in getAllSwatches

swatches

An object keyed by the qualities of colors defined by the Android Palette Class. The keys are the following:

  • "Vibrant"
  • "Vibrant Dark"
  • "Vibrant Light"
  • "Muted"
  • "Muted Dark"
  • "Muted Light"

The values are swatches (possibly null) or with the fields defined below.

Swatch Fields

Colors include alpha in the react-native rgba(255,255,255,1.0) format. Note that on iOS10 devices UIExtendedSRGBColorSpace color values may be greater than 255 or less than 0, but they will render correctly on the device.

Field | Info ------ | ---- color | The main color of the swatch. population | The dominance of this swatch in the image. You can sort on this field to find the most dominant swatch. Android: A positive integer. iOS: A floating point number between 0 and 1. titleTextColor | A text color which contrasts well with the main swatch color for use in titles. bodyTextColor | A text color which contrasts well with the main swatch color for use as body text. swatchInfo | A string encapsulating all the above and more. Can be used for debugging. Android: Note that the hex strings are in the format #aarrggbb rather than the react-native format. iOS: the result string returned by the old (react-native-color-grabber) API. hex | Hex code, computed from color, in format #rrggbbaa .

Example


  import {getAllSwatches} from 'react-native-palette';
  import ImagePicker from 'react-native-image-picker'

  ImagePicker.launchImageLibrary({}, (response)  => {
    var path =  Platform.OS === 'ios' ? response.origURL : response.path;
    getAllSwatches({}, path, (error, swatches) => {
      if (error) {
        console.log(error);
      } else {
        swatches.sort((a, b) => {
          return b.population - a.population;
        });
        swatches.forEach((swatch) => {
          console.log(swatch.swatchInfo);
        });
      }
    });
  });

Contributing

Thanks for your interest in improving react-native-palette. Please review the following requests before submitting your PR.

  1. Test your change with the example app. The README file in the example directory explains an easy way to build and run it.
  2. If you're adding a new feature, please add something in the example app to demonstrate it.
  3. Please keep your changes concise and avoid unnecessary formatting/indentation changes. They make the diff difficult to understand and review. If you have an new idea for formatting that will save the world, by all means propose it, but at least include a prettier spec for automating it. This goes double for Java/Objective-C changes, since those are foreign languages to me.
  4. In your PR, please give a brief explanation of the purpose, referencing Issues when appropriate, and mention whether your change will require a patch, minor or major semver bump.
  5. Please document your changes in CHANGELOG.md.

Thanks again.