Git Product home page Git Product logo

react-native-image-colors's Introduction

react-native-image-colors

Platform TypeScript NPM Badge 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 and UIImageColors on iOS.

Installation

$ npm install react-native-image-colors

or

$ yarn add react-native-image-colors

Android

Rebuild the app.

iOS

Install the pod, then rebuild the app.

npx pod-install

RN < 0.62: if you face a compilation error while building, your Xcode project likely does not support Swift which this package requires. You can fix this by either a) Creating a blank dummy swift file using Xcode or b) Following steps 1,2,3 here.

Usage

Start by importing the module

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

๐ŸŽจ Fetch colors

const colors = await ImageColors.getColors(URI, config)

URI

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

Property Description Type Required Default
fallback If a color property couldn't be retrieved, it will default to this hex color string (note: do not use shorthand hex. e.g. #fff). string No "#000000"
pixelSpacing (Android only) How many pixels to skip when iterating over image pixels. Higher means better performance (note: value cannot be lower than 1). number No 5
quality (iOS only) Highest implies no downscaling and very good colors, but it is very slow. See UIImageColors 'lowest'
'low'
'high'
'highest'
No "low"

Result (android)

On android, you will get an object with the following color properties, plus a platform key to help you figure out that this is the android result type.

Property Type
dominant string
average string
vibrant string
darkVibrant string
lightVibrant string
darkMuted string
lightMuted string
muted string
platform "android"

Result (iOS)

On iOS, you get the following color properties object, plus the respective platform key.

Property Type
background string
primary string
secondary string
detail string
platform "ios"

Example

const coolImage = require("./cool.jpg")

const colors = await ImageColors.getColors(coolImage, {
  fallback: "#228B22",
})

if (colors.platform === "android") {
  // Access android properties
  // e.g.
  const averageColor = colors.average
} else {
  // Access iOS properties
  // e.g.
  const backgroundColor = colors.background
}

Notes

  • There is an example react-native project.

react-native-image-colors's People

Contributors

osamaqarem avatar dependabot[bot] avatar mohit23x avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.