Code Monkey home page Code Monkey logo

borealis's Introduction

Borealis

Borealis finds the most prominent colors in an image, attempting to find colors that are distinct from one another.

It provides defaults which favor speed, but the defaults are easily changed if you prefer accuracy. For most purposes, the default settings should be accurate enough.

Installation

Add this line to your application's Gemfile:

gem 'borealis'

And then execute:

$ bundle

Or install it yourself as:

$ gem install borealis

Usage

Find the three prominent colors in an image:

Borealis.new('image/path.jpg').colors # Returns 3 colors with hex and rgb attributes
Borealis.new('image/path.jpg').hexes # Returns 3 hex values
Borealis.new('image/path.jpg').rgbs # Returns 3 rgb values

Easily get color values out of a color:

color = Borealis.new(file).colors.first
color.rgb # "(222, 2, 15)"
color.hex # "#DE020F"

Measure distance between colors:

orange.rgb_distance_to red #=> 127.0
orange.lab_distance_to red #=> 39.79252198414681

maroon.rgb_distance_to dark_red #=> 11.0
maroon.lab_distance_to dark_read #=> 5.071593528474779

Calculate CIE-L*a*b* value of an sRGB color:

cyan = Borealis::Color.new(0, 255, 255)
cyan.to_lab #=> [91.11652110946342, -48.079618466228716, -14.138127754846131]

Change the defaults:

borealis = Borealis.new('image/path.jpg',
  size: '150x150',
  colors: 3,
  delta: 0.001,
  static: false
)

Size

Default: '50x50'

Size takes any Image Geometry that imagemagick handles.

For more accurate results, use a larger image. However, this comes at a severe time cost.

Colors

Default: 3

The number of colors you want returned.

Iterations

Default: 3

Static

Default: true

Static determines whether the color output will be the same each time given the same image. Use false if you want the colors to change each run.

How does it work?

Borealis uses the k-means clustering algorithm, which in this case:

  1. Takes n colors from the image as cluster centers
  2. Adds the rest of the colors to the cluster which they are closest to
  3. Finds the average color in each cluster, setting the results as new cluster centers
  4. Empties each cluster, except for the new center, and repeats steps 2-3 until convergance has been reached

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

Borealis is copyright 2013 Josh Steiner and thoughtbot, inc., and may be redistributed under the terms specified in the LICENSE file.

borealis's People

Contributors

calebhearth avatar jsteiner avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

borealis's Issues

Convert all colors ahead of time

In the interest of performance, let's convert colors ahead of time and use OOP so that we don't have to convert for each distance calculation, which is going to be very slow.

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.