Socket
Socket
Sign inDemoInstall

react-native-image-colors

Package Overview
Dependencies
582
Maintainers
1
Versions
38
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-native-image-colors

Fetch prominent colors from an image.


Version published
Weekly downloads
27K
decreased by-14.35%
Maintainers
1
Install size
21.6 MB
Created
Weekly downloads
 

Readme

Source

react-native-image-colors

Platform NPM Badge Publish size PRs Welcome

Fetch prominent colors from an image.

Example 1 Demo 1 Android Demo 1 iOS Demo 2 Android Demo 2 iOS

This module is a wrapper around the Palette class on Android, UIImageColors on iOS and node-vibrant for the web.

Installation

npm install react-native-image-colors
yarn add react-native-image-colors

Android

Rebuild the app.

iOS

Install the pod, then rebuild the app.

npx pod-install

Expo

Build custom native code

expo prebuild

# iOS
expo run:ios

# Android
expo run:android

The example is an expo app.

Web

You're good to go!

Usage

import ImageColors from 'react-native-image-colors'

const uri = require('./cool.jpg')

const result = await ImageColors.getColors(uri, {
  fallback: '#228B22',
  cache: true,
  key: 'unique_key',
})

switch (result.platform) {
  case 'android':
    // android result properties
    const vibrantColor = result.vibrant
    break
  case 'web':
    // web result properties
    const lightVibrantColor = result.lightVibrant
    break
  case 'ios':
    // iOS result properties
    const primaryColor = result.primary
    break
  default:
    throw new Error('Unexpected platform key')
}

API

ImageColors.getColors(uri: string, config?: Config): Promise<ImageColorsResult>
uri

A string which can be:

  • URL:

    https://i.imgur.com/O3XSdU7.jpg

  • Local file:

    const catImg = require('./images/cat.jpg')
    
  • Base64:

    const catImgBase64 = 'data:image/jpeg;base64,/9j/4Ri...'
    

    The mime type prefix for base64 is required (e.g. data:image/png;base64).

Config
PropertyDescriptionTypeRequiredDefaultSupported platforms
fallbackIf a color property couldn't be retrieved, it will default to this hex color string (note: do not use shorthand hex. e.g. #fff).stringNo"#000000"All
cacheEnables in-memory caching of the result - skip downloading the same image next time.booleanNofalseAll
keyUnique key to use for the cache entry. The image URI is used as the unique key by default. You should explicitly pass a key if you enable caching and you're using a base64 string as the URI.stringNoundefinedAll
headersHTTP headers to be sent along with the GET request to download the imageRecord<string, string>NoundefinediOS, Android
pixelSpacingHow many pixels to skip when iterating over image pixels. Higher means better performance (note: value cannot be lower than 1).numberNo5Android
qualityHighest implies no downscaling and very good colors.'lowest'
'low'
'high'
'highest'
No"low"iOS, Web

ImageColorsResult

Every result object contains a respective platform key to help narrow down the type.

AndroidImageColors
PropertyType
dominantstring?
averagestring?
vibrantstring?
darkVibrantstring?
lightVibrantstring?
darkMutedstring?
lightMutedstring?
mutedstring?
platform"android"
WebImageColors
PropertyType
dominantstring?
vibrantstring?
darkVibrantstring?
lightVibrantstring?
darkMutedstring?
lightMutedstring?
mutedstring?
platform"web"
IOSImageColors
PropertyType
backgroundstring
primarystring
secondarystring
detailstring
platform"ios"

Notes

  • There is an example app.
  • Since the implementation of each platform is different you can get different color results for each.

Keywords

FAQs

Last updated on 06 May 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc