Skip to content

Commit aaa9b0c

Browse files
committed
Add API docs with YARD (jfairbank#4)
1 parent ee5caab commit aaa9b0c

25 files changed

+507
-0
lines changed

.yardopts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--markup=markdown

lib/chroma.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,75 @@
3838
# Extensions
3939
require 'chroma/extensions/string'
4040

41+
# The main module.
4142
module Chroma
4243
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}
4359
def paint(input)
4460
Color.new(input)
4561
end
4662

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
4773
def hex_from_name(name)
4874
named_colors_map[name]
4975
end
5076

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
5188
def name_from_hex(hex)
5289
hex_named_colors_map[hex]
5390
end
5491

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
55110
def define_palette(name, &block)
56111
if Harmonies.method_defined? name
57112
raise Errors::PaletteDefinedError, "Palette `#{name}' already exists"

lib/chroma/color.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,83 @@
11
module Chroma
2+
# The main class to represent colors.
23
class Color
34
include Attributes
45
include Serializers
56
include Modifiers
67
include Helpers::Bounders
78

9+
# @param input [String, ColorModes::Rgb, ColorModes::Hsl, ColorModes::Hsv]
10+
# @param format [Symbol] the color mode format
811
def initialize(input, format = nil)
912
@input = input
1013
@rgb, gen_format = generate_rgb_and_format(input)
1114
@format = format || gen_format
1215
end
1316

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]
1426
def paint
1527
self
1628
end
1729

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]
1843
def eql?(other)
1944
self.class == other.class && self == other
2045
end
2146

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]
2259
def ==(other)
2360
to_hex == other.to_hex
2461
end
2562

63+
# Returns the complementary color.
64+
#
65+
# @example
66+
# 'red'.paint.complement #=> cyan
67+
#
68+
# @return [Color] the complementary color
2669
def complement
2770
hsl = self.hsl
2871
hsl.h = (hsl.h + 180) % 360
2972
self.class.new(hsl, @format)
3073
end
3174

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]
3281
def palette
3382
Harmonies.new(self)
3483
end

lib/chroma/color/attributes.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,49 @@
11
module Chroma
22
class Color
3+
# Attribute methods for {Color}.
34
module Attributes
45
attr_reader :format
56

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]
614
def dark?
715
brightness < 128
816
end
917

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]
1025
def light?
1126
!dark?
1227
end
1328

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]
1436
def alpha
1537
@rgb.a
1638
end
1739

40+
# Calculates the brightness.
41+
#
42+
# @example
43+
# 'red'.paint.brightness #=> 76.245
44+
# 'yellow'.paint.brightness #=> 225.93
45+
#
46+
# @return [Float]
1847
def brightness
1948
(@rgb.r * 299 + @rgb.g * 587 + @rgb.b * 114) / 1000.0
2049
end

lib/chroma/color/modifiers.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
module Chroma
22
class Color
3+
# Methods that return a new modified {Color}.
34
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]
413
def lighten(amount = 10)
514
hsl = self.hsl
615
hsl.l = clamp01(hsl.l + amount / 100.0)
716
self.class.new(hsl, @format)
817
end
918

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]
1027
def brighten(amount = 10)
1128
# Don't include alpha
1229
rgb = @rgb.to_a[0..2].map(&:round)
@@ -19,30 +36,69 @@ def brighten(amount = 10)
1936
self.class.new(ColorModes::Rgb.new(*rgb), @format)
2037
end
2138

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]
2247
def darken(amount = 10)
2348
hsl = self.hsl
2449
hsl.l = clamp01(hsl.l - amount / 100.0)
2550
self.class.new(hsl, @format)
2651
end
2752

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]
2861
def desaturate(amount = 10)
2962
hsl = self.hsl
3063
hsl.s = clamp01(hsl.s - amount / 100.0)
3164
self.class.new(hsl, @format)
3265
end
3366

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]
3475
def saturate(amount = 10)
3576
hsl = self.hsl
3677
hsl.s = clamp01(hsl.s + amount / 100.0)
3778
self.class.new(hsl, @format)
3879
end
3980

81+
# Converts the color to grayscale.
82+
#
83+
# @example
84+
# 'green'.paint.greyscale #=> #404040
85+
#
86+
# @return [Color]
4087
def grayscale
4188
desaturate(100)
4289
end
4390

4491
alias_method :greyscale, :grayscale
4592

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]
46102
def spin(amount)
47103
hsl = self.hsl
48104
hue = (hsl.h.round + amount) % 360

0 commit comments

Comments
 (0)