Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jfairbank committed Jan 11, 2015
0 parents commit a491d82
Show file tree
Hide file tree
Showing 31 changed files with 1,083 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
*.bundle
*.so
*.o
*.a
mkmf.log
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in colortastic.gemspec
gemspec
13 changes: 13 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright (c) 2015, Jeremy Fairbank <[email protected]>

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
75 changes: 75 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Chroma

Chroma is a color manipulation and palette generation library. It is heavily
inspired by and a very close Ruby port of the
[tinycolor.js](https://bgrins.github.io/TinyColor/)
library. Many thanks to [Brian Grinstead](http://www.briangrinstead.com/blog/)
for his hard work on that library.

Chroma is in alpha stage at the moment. Most of the API methods from tinycolor
have been ported over with a few exceptions. However, I will be working on
cleaning up the API where needed, adding docs and examples, and adding tests
before a first release. Because this is currently in alpha, please be
prepared for possible API changes or bugs.

Please don't hesitate to examine the code and make issues or pull requests
where you feel it is necessary. Please refer to the
[Contributing](#contributing) section below.

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'chroma', '0.0.1.alpha.1'
```

And then execute:

$ bundle

Or install it yourself as:

$ gem install chroma

## Usage

Chroma adds several of the methods available in
[tinycolor.js](https://github.com/bgrins/TinyColor) but renamed appropriately
for Ruby conventions (i.e. `isDark` becomes `dark?`).

To create a color, just call the `Chroma.paint` method, passing in a string
that represents a color.

```ruby
Chroma.paint 'red' # named colors
Chroma.paint '#00ff00' # 6 character hexadecimal
Chroma.paint '#00f' # 3 character hexadecimal
Chroma.paint 'rgb(255, 255, 0)' # rgb
Chroma.paint 'rgba(255, 255, 0, 0.5)' # rgba
Chroma.paint 'hsl(60, 100%, 50%)' # hsl with percentages
Chroma.paint 'hsl(60, 1, 0.5)' # hsl with decimals
Chroma.paint 'hsv(60, 100%, 50%)' # hsv with percentages
Chroma.paint 'hsv(60, 1, 0.5)' # hsv with decimals
```

To work directly from a string you can also use the `String#paint` method:

```ruby
'red'.paint
'#00f'.paint
'rgb(255, 255, 0)'.paint

# etc...
```

## Contributing

Please branch from **dev** for all pull requests.

1. Fork it (https://github.com/jfairbank/chroma/fork)
2. Checkout dev (`git checkout dev`)
3. Create your feature branch (`git checkout -b my-new-feature`)
4. Commit your changes (`git commit -am 'Add some feature'`)
5. Push to the branch (`git push origin my-new-feature`)
6. Create a new pull request against dev
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require "bundler/gem_tasks"

23 changes: 23 additions & 0 deletions chroma.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'chroma/version'

Gem::Specification.new do |spec|
spec.name = 'chroma'
spec.version = Chroma::VERSION
spec.authors = ['Jeremy Fairbank']
spec.email = ['[email protected]']
spec.summary = %q{Color manipulation and palette generation.}
spec.description = %q{Chroma is a color manipulation and palette generation gem.}
spec.homepage = 'https://github.com/jfairbank/chroma'
spec.license = 'MIT'

spec.files = `git ls-files -z`.split("\x0")
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']

spec.add_development_dependency 'bundler', '~> 1.7'
spec.add_development_dependency 'rake', '~> 10.0'
end
63 changes: 63 additions & 0 deletions lib/chroma.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# General
require 'chroma/version'
require 'yaml'

# Modules
require 'chroma/helpers/bounders'

# Color
require 'chroma/color/attributes'
require 'chroma/color/serializers'
require 'chroma/color/modifiers'
require 'chroma/color'
require 'chroma/color_modes'

# Palettes
require 'chroma/harmonies'

# RGB Generators
require 'chroma/rgb_generator'
require 'chroma/rgb_generator/base'
require 'chroma/rgb_generator/from_string'
require 'chroma/rgb_generator/from_rgb_values'
require 'chroma/rgb_generator/from_rgb'
require 'chroma/rgb_generator/from_hsl_values'
require 'chroma/rgb_generator/from_hsl'
require 'chroma/rgb_generator/from_hsv_values'
require 'chroma/rgb_generator/from_hsv'
require 'chroma/rgb_generator/from_hex_string_values'

# Converters
require 'chroma/converters/base'
require 'chroma/converters/rgb_converter'
require 'chroma/converters/hsl_converter'
require 'chroma/converters/hsv_converter'

# Extensions
require 'chroma/extensions/string'

module Chroma
class << self
def paint(input)
Color.new(input)
end

def hex_from_name(name)
named_colors_map[name]
end

def name_from_hex(hex)
hex_named_colors_map[hex]
end

private

def hex_named_colors_map
@hex_named_colors_map ||= named_colors_map.invert
end

def named_colors_map
@named_colors ||= YAML.load_file(File.expand_path('../support/named_colors.yml', __FILE__))
end
end
end
34 changes: 34 additions & 0 deletions lib/chroma/color.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Chroma
class Color
include Attributes
include Serializers
include Modifiers
include Helpers::Bounders

def initialize(input, format = nil)
@input = input
@rgb, gen_format = generate_rgb_and_format(input)
@format = format || gen_format
end

def complement
hsl = to_hsl
hsl.h = (hsl.h + 180) % 360
Color.new(hsl)
end

def palette
Harmonies.new(self)
end

private

def to_2char_hex(n)
n.round.to_s(16).rjust(2, '0')
end

def generate_rgb_and_format(input)
RgbGenerator.generate_rgb_and_format(input)
end
end
end
21 changes: 21 additions & 0 deletions lib/chroma/color/attributes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Chroma
class Color
module Attributes
def dark?
brightness < 128
end

def light?
!dark?
end

def alpha
@rgb.a
end

def brightness
(@rgb.r * 299 + @rgb.g * 587 + @rgb.b * 114) / 1000.0
end
end
end
end
52 changes: 52 additions & 0 deletions lib/chroma/color/modifiers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module Chroma
class Color
module Modifiers
def lighten(amount = 10)
hsl = to_hsl
hsl.l = clamp01(hsl.l + amount / 100.0)
self.class.new(hsl, @format)
end

def brighten(amount = 10)
# Don't include alpha
rgb = @rgb.to_a[0..2].map(&:round)
amount = (255 * (-amount / 100.0)).round

rgb.map! do |n|
[0, [255, n - amount].min].max
end

self.class.new(ColorModes::Rgb.new(*rgb), @format)
end

def darken(amount = 10)
hsl = to_hsl
hsl.l = clamp01(hsl.l - amount / 100.0)
self.class.new(hsl, @format)
end

def desaturate(amount = 10)
hsl = to_hsl
hsl.s = clamp01(hsl.s - amount / 100.0)
self.class.new(hsl, @format)
end

def saturate(amount = 10)
hsl = to_hsl
hsl.s = clamp01(hsl.s + amount / 100.0)
self.class.new(hsl, @format)
end

def greyscale
desaturate(100)
end

def spin(amount)
hsl = to_hsl
hue = (hsl.h.round + amount) % 360
hsl.h = hue < 0 ? 360 + hue : hue
self.class.new(hsl, @format)
end
end
end
end
Loading

0 comments on commit a491d82

Please sign in to comment.