chroma 0.0.1.alpha.2 → 0.0.1.alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 83b41e45a991b08c177b51e08ad7147ff5f22d60
4
- data.tar.gz: b55cf5c98e93305486a863a38cc1642fd695b57a
3
+ metadata.gz: 763b7f5b0794c7dc09cd95962cd7735112f6d097
4
+ data.tar.gz: 03ff16e0f2504635475ae9d310ca3d8aef338fd3
5
5
  SHA512:
6
- metadata.gz: c6a283052b0210c1434cc8492063bf95a7653bf28c08111e840b9a0d77d936d29c6b761498bdcc940ad62e5f579df217311cf740c12d8ca7023780795571106c
7
- data.tar.gz: 8e61b3b0060212abcab4ba2de3d1a3d700c91ecd6dc1b8dec3f154fa954e39149f6d7ee73627b085cd4efe245162954424e68632967f7df4317185ac33ebf807
6
+ metadata.gz: 5fd92090950985a25e34d1ecf90387cc1e409473e9907f153e31c6b41aff2be6f835eb3b1a0ff69e6308f427b424ac62f1c803fc53d34bc21e45679f55f25dd3
7
+ data.tar.gz: 64e588328d689f4774d9154681549b80dee94f2944686b97f8029a136d0b796dc1427097ff45fe8d19194913d91061e957bcf1b09eac778f4c003383754746c9
@@ -0,0 +1 @@
1
+ --markup=markdown
@@ -1,5 +1,33 @@
1
- ### v0.0.1.alpha.2
1
+ ### [v0.0.1.alpha.3] - 2015-01-13
2
+
3
+ **Bug Fixes:**
4
+
5
+ * Fix bug where `Color#complement` didn't return a color of the same format.
6
+ * Fix bug where palettes did not have the same format as the seed color. (#16)
7
+ * Fix bug where the helper method `bound01` was not always producing accurate
8
+ results for percentages due to integer division.
9
+ * Fix bug where a `Color` created from an rgba string
10
+ (e.g. `'rgba(255, 0, 0, 0.5).paint'`) was not serializing to an rgba string
11
+ from `to_s`. (#17)
12
+
13
+ **Method Changes:**
14
+
15
+ * Add `Color#format` method to return `@format` instance variable.
16
+ * Change arguments for `analogous` and `monochromatic` to option arguments.
17
+ * Add ability to output palette as an array of color format strings via the
18
+ `:as` option. (#10)
19
+ * On `Color` rename `greyscale` to `grayscale` and alias `greyscale` back
20
+ to `grayscale`.
21
+
22
+ **Miscellaneous Changes:**
23
+
24
+ * Introduced custom errors and replaced `raise` calls with them.
25
+ * Added API doc headers. (#4)
26
+
27
+ ### [v0.0.1.alpha.2] - 2015-01-13
28
+
2
29
  **Bug Fixes:**
30
+
3
31
  * Fixed bug with number of arguments passed to generator classes in RgbGenerator. (#1)
4
32
  * Make `FromHexStringValues.from_hex8` take alpha as second parameter instead of last.
5
33
  Fixes incorrect color generation from hex8. (#6)
@@ -14,6 +42,7 @@
14
42
  wrong alpha value. (#15)
15
43
 
16
44
  **Method Changes:**
45
+
17
46
  * Add optional `hex_for_unknown` parameter to `Color::Serializers#to_name`.
18
47
  If true, it allows `to_name` to default to hex string if name is not found
19
48
  instead of returning `'<unknown>'`. (#2)
@@ -35,3 +64,7 @@
35
64
  * `to_rgb` -> `rgb` (moved attr_reader to serializers and made public)
36
65
  * `to_rgb_s` -> `to_rgb`
37
66
  * Removed `to_name_s` alias
67
+
68
+ ### [v0.0.1.alpha.1] - 2015-01-11
69
+
70
+ * Initial release
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Chroma
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/chroma.svg)](http://badge.fury.io/rb/chroma)
4
+
3
5
  Chroma is a color manipulation and palette generation library. It is heavily
4
6
  inspired by and a very close Ruby port of the
5
7
  [tinycolor.js](https://bgrins.github.io/TinyColor/)
@@ -21,7 +23,7 @@ where you feel it is necessary. Please refer to the
21
23
  Add this line to your application's Gemfile:
22
24
 
23
25
  ```ruby
24
- gem 'chroma', '0.0.1.alpha.1'
26
+ gem 'chroma', '0.0.1.alpha.3'
25
27
  ```
26
28
 
27
29
  And then execute:
data/Rakefile CHANGED
@@ -1,2 +1,9 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
2
 
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ task default: :spec
7
+ rescue LoadError
8
+ # Rspec not available
9
+ end
@@ -1,5 +1,6 @@
1
1
  # General
2
2
  require 'chroma/version'
3
+ require 'chroma/errors'
3
4
  require 'yaml'
4
5
 
5
6
  # Modules
@@ -37,22 +38,80 @@ require 'chroma/converters/hsv_converter'
37
38
  # Extensions
38
39
  require 'chroma/extensions/string'
39
40
 
41
+ # The main module.
40
42
  module Chroma
41
43
  class << self
44
+ # Returns a new instance of color. Supports hexadecimal, rgb, rgba, hsl,
45
+ # hsla, hsv, hsva, and named color formats.
46
+ #
47
+ # @api public
48
+ #
49
+ # @example
50
+ # Chroma.paint('red')
51
+ # Chroma.paint('#f00')
52
+ # Chroma.paint('#ff0000')
53
+ # Chroma.paint('rgb(255, 0, 0)')
54
+ # Chroma.paint('hsl(0, 100%, 50%)')
55
+ # Chroma.paint('hsv(0, 100%, 100%)')
56
+ #
57
+ # @param input [String] the color
58
+ # @return [Color] an instance of {Color}
42
59
  def paint(input)
43
60
  Color.new(input)
44
61
  end
45
62
 
63
+ # Returns the hexadecimal string representation of a named color and nil
64
+ # if no match is found. Favors 3-character hexadecimal if possible.
65
+ #
66
+ # @example
67
+ # Chroma.hex_from_name('red') #=> 'f00'
68
+ # Chroma.hex_from_name('aliceblue') #=> 'f0f8ff'
69
+ # Chroma.hex_from_name('foo') #=> nil
70
+ #
71
+ # @param name [String] the color name
72
+ # @return [String, nil] the color as a string hexadecimal or nil
46
73
  def hex_from_name(name)
47
74
  named_colors_map[name]
48
75
  end
49
76
 
77
+ # Returns the color name of a hexadecimal color if available and nil if no
78
+ # match is found. Requires 3-character hexadecimal input for applicable
79
+ # colors.
80
+ #
81
+ # @example
82
+ # Chroma.name_from_hex('f00') #=> 'red'
83
+ # Chroma.name_from_hex('f0f8ff') #=> 'aliceblue'
84
+ # Chroma.name_from_hex('123123') #=> nil
85
+ #
86
+ # @param hex [String] the hexadecimal color
87
+ # @return [String, nil] the color name or nil
50
88
  def name_from_hex(hex)
51
89
  hex_named_colors_map[hex]
52
90
  end
53
91
 
92
+ # Defines a custom palette for use by {Color#palette}. Uses a DSL inside
93
+ # `block` that mirrors the methods in {Color::Modifiers}.
94
+ #
95
+ # @example
96
+ # 'red'.paint.palette.respond_to? :my_palette #=> false
97
+ #
98
+ # Chroma.define_palette :my_palette do
99
+ # spin 60
100
+ # spin 120
101
+ # spin 240
102
+ # end
103
+ #
104
+ # 'red'.paint.palette.respond_to? :my_palette #=> true
105
+ #
106
+ # @param name [Symbol, String] the name of the custom palette
107
+ # @param block [Proc] the palette definition block
108
+ # @raise [Errors::PaletteDefinedError] if the palette is already defined
109
+ # @return [Symbol, String] the name of the custom palette
54
110
  def define_palette(name, &block)
55
- raise "Palette `#{name}' already exists" if Harmonies.method_defined? name
111
+ if Harmonies.method_defined? name
112
+ raise Errors::PaletteDefinedError, "Palette `#{name}' already exists"
113
+ end
114
+
56
115
  PaletteBuilder.build(name, &block)
57
116
  end
58
117
 
@@ -1,34 +1,83 @@
1
1
  module Chroma
2
+ # The main class to represent colors.
2
3
  class Color
3
4
  include Attributes
4
5
  include Serializers
5
6
  include Modifiers
6
7
  include Helpers::Bounders
7
8
 
9
+ # @param input [String, ColorModes::Rgb, ColorModes::Hsl, ColorModes::Hsv]
10
+ # @param format [Symbol] the color mode format
8
11
  def initialize(input, format = nil)
9
12
  @input = input
10
13
  @rgb, gen_format = generate_rgb_and_format(input)
11
14
  @format = format || gen_format
12
15
  end
13
16
 
17
+ # Returns self. Useful for ducktyping situations with {String#paint}.
18
+ #
19
+ # @example
20
+ # red = 'red'.paint
21
+ #
22
+ # red.paint #=> red
23
+ # red.paint.equal? red #=> true
24
+ #
25
+ # @return [self]
14
26
  def paint
15
27
  self
16
28
  end
17
29
 
30
+ # Returns true if `self` is equal to `other` and they're both instances of
31
+ # {Color}.
32
+ #
33
+ # @example
34
+ # red = 'red'.paint
35
+ # blue = 'blue'.paint
36
+ #
37
+ # red.eql? red #=> true
38
+ # red.eql? blue #=> false
39
+ # red.eql? '#f00'.paint #=> true
40
+ #
41
+ # @param other [Color]
42
+ # @return [true, false]
18
43
  def eql?(other)
19
44
  self.class == other.class && self == other
20
45
  end
21
46
 
47
+ # Returns true if both are equal in value.
48
+ #
49
+ # @example
50
+ # red = 'red'.paint
51
+ # blue = 'blue'.paint
52
+ #
53
+ # red == red #=> true
54
+ # red == blue #=> false
55
+ # red == '#f00'.paint #=> true
56
+ #
57
+ # @param other [Color]
58
+ # @return [true, false]
22
59
  def ==(other)
23
60
  to_hex == other.to_hex
24
61
  end
25
62
 
63
+ # Returns the complementary color.
64
+ #
65
+ # @example
66
+ # 'red'.paint.complement #=> cyan
67
+ #
68
+ # @return [Color] the complementary color
26
69
  def complement
27
70
  hsl = self.hsl
28
71
  hsl.h = (hsl.h + 180) % 360
29
- Color.new(hsl)
72
+ self.class.new(hsl, @format)
30
73
  end
31
74
 
75
+ # Returns an instance of {Harmonies} from which to call a palette method.
76
+ #
77
+ # @example
78
+ # 'red'.paint.palette #=> #<Chroma::Harmonies:0x007faf6b9f9148 @color=red>
79
+ #
80
+ # @return [Harmonies]
32
81
  def palette
33
82
  Harmonies.new(self)
34
83
  end
@@ -1,18 +1,49 @@
1
1
  module Chroma
2
2
  class Color
3
+ # Attribute methods for {Color}.
3
4
  module Attributes
5
+ attr_reader :format
6
+
7
+ # Determines if the color is dark.
8
+ #
9
+ # @example
10
+ # 'red'.paint.dark? #=> true
11
+ # 'yellow'.paint.dark? #=> false
12
+ #
13
+ # @return [true, false]
4
14
  def dark?
5
15
  brightness < 128
6
16
  end
7
17
 
18
+ # Determines if the color is light.
19
+ #
20
+ # @example
21
+ # 'red'.paint.light? #=> false
22
+ # 'yellow'.paint.light? #=> true
23
+ #
24
+ # @return [true, false]
8
25
  def light?
9
26
  !dark?
10
27
  end
11
28
 
29
+ # Returns the alpha channel value.
30
+ #
31
+ # @example
32
+ # 'red'.paint.alpha #=> 1.0
33
+ # 'rgba(0, 0, 0, 0.5)'.paint.alpha #=> 0.5
34
+ #
35
+ # @return [Float]
12
36
  def alpha
13
37
  @rgb.a
14
38
  end
15
39
 
40
+ # Calculates the brightness.
41
+ #
42
+ # @example
43
+ # 'red'.paint.brightness #=> 76.245
44
+ # 'yellow'.paint.brightness #=> 225.93
45
+ #
46
+ # @return [Float]
16
47
  def brightness
17
48
  (@rgb.r * 299 + @rgb.g * 587 + @rgb.b * 114) / 1000.0
18
49
  end
@@ -1,12 +1,29 @@
1
1
  module Chroma
2
2
  class Color
3
+ # Methods that return a new modified {Color}.
3
4
  module Modifiers
5
+ # Lightens the color by the given `amount`.
6
+ #
7
+ # @example
8
+ # 'red'.paint.lighten #=> #ff3333
9
+ # 'red'.paint.lighten(20) #=> #ff6666
10
+ #
11
+ # @param amount [Fixnum]
12
+ # @return [Color]
4
13
  def lighten(amount = 10)
5
14
  hsl = self.hsl
6
15
  hsl.l = clamp01(hsl.l + amount / 100.0)
7
16
  self.class.new(hsl, @format)
8
17
  end
9
18
 
19
+ # Brightens the color by the given `amount`.
20
+ #
21
+ # @example
22
+ # 'red'.paint.brighten #=> #ff1919
23
+ # 'red'.paint.brighten(20) #=> #ff3333
24
+ #
25
+ # @param amount [Fixnum]
26
+ # @return [Color]
10
27
  def brighten(amount = 10)
11
28
  # Don't include alpha
12
29
  rgb = @rgb.to_a[0..2].map(&:round)
@@ -19,28 +36,69 @@ module Chroma
19
36
  self.class.new(ColorModes::Rgb.new(*rgb), @format)
20
37
  end
21
38
 
39
+ # Darkens the color by the given `amount`.
40
+ #
41
+ # @example
42
+ # 'red'.paint.darken #=> #cc0000
43
+ # 'red'.paint.darken(20) #=> #990000
44
+ #
45
+ # @param amount [Fixnum]
46
+ # @return [Color]
22
47
  def darken(amount = 10)
23
48
  hsl = self.hsl
24
49
  hsl.l = clamp01(hsl.l - amount / 100.0)
25
50
  self.class.new(hsl, @format)
26
51
  end
27
52
 
53
+ # Desaturates the color by the given `amount`.
54
+ #
55
+ # @example
56
+ # 'red'.paint.desaturate #=> #f20d0d
57
+ # 'red'.paint.desaturate(20) #=> #e61919
58
+ #
59
+ # @param amount [Fixnum]
60
+ # @return [Color]
28
61
  def desaturate(amount = 10)
29
62
  hsl = self.hsl
30
63
  hsl.s = clamp01(hsl.s - amount / 100.0)
31
64
  self.class.new(hsl, @format)
32
65
  end
33
66
 
67
+ # Saturates the color by the given `amount`.
68
+ #
69
+ # @example
70
+ # '#123'.paint.saturate #=> #0e2236
71
+ # '#123'.paint.saturate(20) #=> #0a223a
72
+ #
73
+ # @param amount [Fixnum]
74
+ # @return [Color]
34
75
  def saturate(amount = 10)
35
76
  hsl = self.hsl
36
77
  hsl.s = clamp01(hsl.s + amount / 100.0)
37
78
  self.class.new(hsl, @format)
38
79
  end
39
80
 
40
- def greyscale
81
+ # Converts the color to grayscale.
82
+ #
83
+ # @example
84
+ # 'green'.paint.greyscale #=> #404040
85
+ #
86
+ # @return [Color]
87
+ def grayscale
41
88
  desaturate(100)
42
89
  end
43
90
 
91
+ alias_method :greyscale, :grayscale
92
+
93
+ # Spins around the hue color wheel by `amount` in degrees.
94
+ #
95
+ # @example
96
+ # 'red'.paint.spin(30) #=> #ff80000
97
+ # 'red'.paint.spin(60) #=> yellow
98
+ # 'red'.paint.spin(90) #=> #80ff00
99
+ #
100
+ # @param amount [Fixnum]
101
+ # @return [Color]
44
102
  def spin(amount)
45
103
  hsl = self.hsl
46
104
  hue = (hsl.h.round + amount) % 360
@@ -1,28 +1,81 @@
1
1
  module Chroma
2
2
  class Color
3
+ # Methods for serializing {Color} to different color mode string formats.
3
4
  module Serializers
5
+ # Convert to hsv string.
6
+ #
7
+ # @example
8
+ # 'red'.paint.to_hsv #=> 'hsv(0, 100%, 100%)'
9
+ # 'rgba(255, 0, 0, 0.5)'.paint.to_hsv #=> 'hsva(0, 100%, 100%, 0.5)'
10
+ #
11
+ # @return [String]
4
12
  def to_hsv
5
13
  to_hs(:v)
6
14
  end
7
15
 
16
+ # Convert to hsl string.
17
+ #
18
+ # @example
19
+ # 'red'.paint.to_hsl #=> 'hsl(0, 100%, 50%)'
20
+ # 'rgba(255, 0, 0, 0.5)'.paint.to_hsl #=> 'hsla(0, 100%, 50%, 0.5)'
21
+ #
22
+ # @return [String]
8
23
  def to_hsl
9
24
  to_hs(:l)
10
25
  end
11
26
 
27
+ # Convert to hexadecimal string.
28
+ #
29
+ # @example
30
+ # 'red'.paint.to_hex #=> '#ff0000'
31
+ # 'red'.paint.to_hex(true) #=> '#f00'
32
+ # 'rgba(255, 0, 0, 0.5)'.paint.to_hex #=> '#ff0000'
33
+ #
34
+ # @param allow_3 [true, false] output 3-character hexadecimal
35
+ # if possible
36
+ # @return [String]
12
37
  def to_hex(allow_3 = false)
13
38
  "##{to_basic_hex(allow_3)}"
14
39
  end
15
40
 
41
+ # Convert to 8-character hexadecimal string. The highest order byte
42
+ # (left most hexadecimal pair represents the alpha value).
43
+ #
44
+ # @example
45
+ # 'red'.paint.to_hex #=> '#ffff0000'
46
+ # 'rgba(255, 0, 0, 0.5)'.paint.to_hex #=> '#80ff0000'
47
+ #
48
+ # @return [String]
16
49
  def to_hex8
17
50
  "##{to_basic_hex8}"
18
51
  end
19
52
 
53
+ # Convert to rgb string.
54
+ #
55
+ # @example
56
+ # 'red'.paint.to_rgb #=> 'rgb(255, 0, 0)'
57
+ # 'rgba(255, 0, 0, 0.5)'.paint.to_rgb #=> 'rgb(255, 0, 0, 0.5)'
58
+ #
59
+ # @return [String]
20
60
  def to_rgb
21
61
  middle = @rgb.to_a[0..2].map(&:round).join(', ')
22
62
 
23
63
  with_alpha(:rgb, middle)
24
64
  end
25
65
 
66
+ # Convert to named color if possible. If a color name can't be found, it
67
+ # returns `'<unknown>'` or the hexadecimal string based on the value of
68
+ # `hex_for_unknown`.
69
+ #
70
+ # @example
71
+ # 'red'.paint.to_name #=> 'red'
72
+ # 'rgba(255, 0, 0, 0.5)'.paint.to_name #=> '<unknown>'
73
+ # '#00f'.paint.to_name #=> 'blue'
74
+ # '#123'.paint.to_name(true) #=> '#112233'
75
+ #
76
+ # @param hex_for_unknown [true, false] determine how unknown color names
77
+ # should be returned
78
+ # @return [String]
26
79
  def to_name(hex_for_unknown = false)
27
80
  return 'transparent' if alpha.zero?
28
81
 
@@ -37,6 +90,16 @@ module Chroma
37
90
  end
38
91
  end
39
92
 
93
+ # Convert to a string based on the color format.
94
+ #
95
+ # @example
96
+ # 'red'.paint.to_s #=> 'red'
97
+ # 'rgb(255, 0, 0)'.paint.to_s #=> 'rgb(255, 0, 0)'
98
+ # '#f00'.paint.to_s #=> '#f00'
99
+ # '#80ff0000'.paint.to_s(:rgb) #=> 'rgba(255, 0, 0, 0.5)'
100
+ #
101
+ # @param format [Symbol] the color format
102
+ # @return [String]
40
103
  def to_s(format = @format)
41
104
  use_alpha = alpha < 1 && alpha >= 0 && /^hex(3|6)?$/ =~ format
42
105
 
@@ -56,14 +119,20 @@ module Chroma
56
119
 
57
120
  alias_method :inspect, :to_s
58
121
 
122
+ # Converts to an instance of {ColorModes::Hsv}
123
+ # @return [ColorModes::Hsv]
59
124
  def hsv
60
125
  Converters::HsvConverter.convert_rgb(@rgb)
61
126
  end
62
127
 
128
+ # Converts to an instance of {ColorModes::Hsl}
129
+ # @return [ColorModes::Hsl]
63
130
  def hsl
64
131
  Converters::HslConverter.convert_rgb(@rgb)
65
132
  end
66
133
 
134
+ # Converts to an instance of {ColorModes::Rgb}
135
+ # @return [ColorModes::Rgb]
67
136
  attr_reader :rgb
68
137
 
69
138
  private