Skip to content

Instantly share code, notes, and snippets.

@koffeinfrei
Created September 13, 2011 20:01
Show Gist options
  • Save koffeinfrei/1214945 to your computer and use it in GitHub Desktop.
Save koffeinfrei/1214945 to your computer and use it in GitHub Desktop.
Codebrawl #10
input.png
/Gemfile.lock

selective-color

Turns the input image (input.png) into a selectively colored image, turning it into grayscale with just the lighter blue crayon left colored.

dependencies

usage

$ bundle install
$ curl -o input.png https://raw.github.com/gist/7addc24b123aad374832/96656b5a75287f8ca8beac523f1d32eee4030aa5/output.png
$ ruby selective_color.rb
source "http://rubygems.org"
gem "chunky_png", "~> 1.2.1"
gem "colour", "~> 0.4.0"
require 'chunky_png'
require 'colour'
#color_range = (14..65) # yellow
color_range = (170..220) # lighter blue
input = ChunkyPNG::Image.from_file('input.png')
output = input.grayscale
input.pixels.each_index do |x|
pixel = input.pixels[x]
hsv = RGB.new(ChunkyPNG::Color.r(pixel), ChunkyPNG::Color.g(pixel), ChunkyPNG::Color.b(pixel)).to_hsv
output.pixels[x] = pixel if color_range.cover? hsv.h
end
output.save('output.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment