sass-cmyk 0.0.1

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.
Files changed (3) hide show
  1. checksums.yaml +15 -0
  2. data/lib/sass-cmyk.rb +164 -0
  3. metadata +78 -0
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ODAyODk2NDRjNzg0MWQ5MWI5NjQ2NDBmNGUyN2EyOTlmZWUyM2E2YQ==
5
+ data.tar.gz: !binary |-
6
+ NzZhNjBiYjBlNWRjMjBiM2JkNTU1MjFlODE5MzI5ZDNiYzIzYWE0MA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MDU3YTM5NGE0YmVjYjZmM2Y2ZTRmZDQyOGMyNDRiOWNiZDFmNTNmMzE3MWFi
10
+ NmNlNWY0MjlkYTg1NWY2M2QxNWJmNzUyOGVmYmNlYTY0ZGYwZGQyZTI0YTA5
11
+ MDY1ZjZjYTlkZmQ1NGQzMTcwZmZiYTZlMmQzZDA3MDc5YjExN2Y=
12
+ data.tar.gz: !binary |-
13
+ ODQ2MzhmOGQ4YTVhOWFkYzgzNTYxYzk4NzdjNTA2YzVlYjY0ZWFhM2Y0MTFh
14
+ OTE1YWNlMTNjYTM3YmIwZDYwZGVmOWU4YTgyM2U2OWUzMWY3ZjkxMjUzY2Jj
15
+ NzA4ODZjZGNmZDdhMmJhZjkwMWUyYjM4M2IwOWNhN2EwYjc4Yjc=
@@ -0,0 +1,164 @@
1
+ require 'sass'
2
+
3
+ module CMYKClass
4
+
5
+ class CMYK < Sass::Script::Value::Base
6
+
7
+ attr_reader :attrs
8
+
9
+ # Attributes specified as a hash, representing CMYK percentages, i.e.
10
+ # Sass::Script::Value::CMYK.new({:cyan=>10, :magenta=>20, :yellow=>30, :black=>40}) is equivalent to cmyk(10%,20%,30%,40%)
11
+ def initialize(cmyk_attrs)
12
+ # Reject all attribute values that are not numbers between 0 and 100
13
+ cmyk_attrs.reject! {|k, v| !(v.class == Fixnum and v.between?(0, 100))}
14
+ raise ArgumentError.new("CMYK Object must be initialized with hash values between 0 and 100 for :cyan, :magenta, :yellow, and :black") unless [:cyan, :magenta, :yellow, :black].all? {|k| cmyk_attrs.key? k}
15
+ @attrs = cmyk_attrs
16
+ end
17
+
18
+ def cyan
19
+ @attrs[:cyan]
20
+ end
21
+
22
+ def magenta
23
+ @attrs[:magenta]
24
+ end
25
+
26
+ def yellow
27
+ @attrs[:yellow]
28
+ end
29
+
30
+ def black
31
+ @attrs[:black]
32
+ end
33
+
34
+ def normalize
35
+ # Return new CMYK object with normalized color components
36
+ new_color_attrs = @attrs.merge(_normalize)
37
+ Sass::Script::Value::CMYK.new(new_color_attrs)
38
+ end
39
+
40
+ def normalize!
41
+ # Normalize color components in place
42
+ @attrs.merge!(_normalize)
43
+ self
44
+ end
45
+
46
+ def _normalize
47
+ # Normalize color components via the following algorithm, per SO (http://stackoverflow.com/a/1530158)
48
+ # C = C - min(C, M, Y)
49
+ # M = M - min(C, M, Y)
50
+ # Y = Y - min(C, M, Y)
51
+ # K = min(100, K + min(C, M, Y))
52
+ cmy_min = [@attrs[:cyan], @attrs[:magenta], @attrs[:yellow]].min
53
+ new_attrs = {:cyan => @attrs[:cyan] - cmy_min,
54
+ :magenta => @attrs[:magenta] - cmy_min,
55
+ :yellow => @attrs[:yellow] - cmy_min,
56
+ :black => [100, @attrs[:black] + cmy_min].min}
57
+ end
58
+
59
+ def plus(other)
60
+ if other.is_a?(Sass::Script::Value::CMYK)
61
+ new_color_attrs = {}
62
+ [:cyan, :magenta, :yellow, :black].each do |component|
63
+ # Add corresponding components of each color, limiting to a max of 100
64
+ component_sum = [self.attrs[component] + other.attrs[component], 100].min
65
+ new_color_attrs[component] = component_sum
66
+ end
67
+ # Make new color from summed componenets
68
+ new_color = Sass::Script::Value::CMYK.new(new_color_attrs)
69
+ # Normalize component values
70
+ new_color.normalize!
71
+ else
72
+ raise ArgumentError.new("Cannot add object of class #{other.class} to CMYK color #{self}. Only CMYK colors can be added to CMYK colors")
73
+ end
74
+ end
75
+
76
+ def minus(other)
77
+ raise NoMethodError.new("Cannot apply subtraction to #{self}. Subtraction not supported for CMYK colors.")
78
+ end
79
+
80
+ # TODO: This does not work commutatively yet; only works for CMYK * scalar, and not scalar * CMYK
81
+ # To add support for scalar * CMYK, need to override "times" instance method on Sass::Script::Value::Number
82
+ def times(other)
83
+ raise ArgumentError.new("Cannot multiply #{self} by #{other}. CMYK colors can only be multiplied by numbers") if !other.is_a?(Sass::Script::Value::Number)
84
+ if other.is_unit?('%')
85
+ scale_factor = other.value.to_f / 100
86
+ else
87
+ scale_factor = other.value
88
+ end
89
+ new_color_attrs = {}
90
+ [:cyan, :magenta, :yellow, :black].each do |component|
91
+ # Scale corresponding components of each color by "scale_factor"
92
+ new_color_attrs[component] = (self.attrs[component] * scale_factor).round
93
+ end
94
+ # Raise error if any resulting component attribute is over 100%, as that would mean it's not possible to scale proportionally
95
+ raise ArgumentError.new("Cannot scale #{self} proportionally by #{other}, as that would result in at least one component over 100%") if new_color_attrs.map {|k, v| v}.max > 100
96
+ # Make new color from scaled components
97
+ new_color = Sass::Script::Value::CMYK.new(new_color_attrs)
98
+ # Normalize component values
99
+ new_color.normalize!
100
+ end
101
+
102
+ def div(other)
103
+ raise ArgumentError.new("Cannot divide #{self} by #{other}. CMYK colors can only be divided by numbers") if !other.is_a?(Sass::Script::Value::Number)
104
+ raise ArgumentError.new("Cannot divide CMYK color #{self} by zero") if other.value == 0
105
+ reciprocal = Sass::Script::Value::Number.new(1.0/other.value)
106
+ self.times(reciprocal)
107
+ end
108
+
109
+ def to_s(opts = {})
110
+ "cmyk(#{@attrs[:cyan]}%,#{@attrs[:magenta]}%,#{@attrs[:yellow]}%,#{@attrs[:black]}%)"
111
+ end
112
+ end
113
+ end
114
+
115
+ module CMYKLibrary
116
+ def cmyk(c, m, y, k)
117
+ cmyk_arr = [[:cyan, c], [:magenta, m], [:yellow, y], [:black, k]].map do |(comp_name, comp_value)|
118
+ assert_type comp_value, :Number, comp_name
119
+ if comp_value.is_unit?("%")
120
+ comp_value_normalized = comp_value.value
121
+ else
122
+ comp_value_normalized = (comp_value.value * 100).round
123
+ end
124
+
125
+ if comp_value_normalized.is_a?(Fixnum) && comp_value_normalized.between?(0, 100)
126
+ [comp_name, comp_value_normalized]
127
+ else
128
+ raise ArgumentError.new("Invalid #{comp_name} value #{comp_value}. Must be a float between 0 and 1 or a percent between 0 and 100.")
129
+ end
130
+ end
131
+
132
+ cmyk_attrs = Hash[cmyk_arr]
133
+
134
+ Sass::Script::Value::CMYK.new(cmyk_attrs)
135
+ end
136
+
137
+ Sass::Script::Functions.declare :cmyk, [:cyan, :magenta, :yellow, :black]
138
+
139
+ def cmyk_mix(cmyk_color1, cmyk_color2)
140
+ raise ArgumentError.new("Bad arguments to cmyk_mix: #{cmyk_color1}, #{cmyk_color2}. cmyk_mix requires two CMYK colors as arguments") unless (cmyk_color1.is_a?(Sass::Script::Value::CMYK) && cmyk_color2.is_a?(Sass::Script::Value::CMYK))
141
+ cmyk_color1.plus(cmyk_color2)
142
+ end
143
+
144
+ Sass::Script::Functions.declare :cmyk_mix, [:cmyk1, :cmyk2]
145
+
146
+ def cmyk_scale(cmyk_color, percent)
147
+ raise ArgumentError.new("Bad argument to cmyk_scale: #{cmyk_color}. First argument must be a CMYK color") unless cmyk_color.is_a?(Sass::Script::Value::CMYK)
148
+ raise ArgumentError.new("Bad argument to cmyk_scale: #{percent}. Second argument must be a percent") unless (percent.is_a?(Sass::Script::Value::Number) && percent.is_unit?('%'))
149
+ cmyk_color.times(percent)
150
+ end
151
+
152
+ Sass::Script::Functions.declare :cmyk_scale, [:cmyk, :percent]
153
+
154
+ end
155
+
156
+ module Sass::Script::Functions
157
+ include CMYKLibrary
158
+ end
159
+
160
+ module Sass::Script::Value
161
+ include CMYKClass
162
+ end
163
+
164
+
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sass-cmyk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sanders Kleinfeld
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sass
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: ! " Need to preprocess CMYK colors with Sass? The sass-cmyk plugin
42
+ lets you construct CMYK color objects with cmyk(), \n adds support for performing
43
+ math operations (+, *, /) on CMYK colors, and provides functions for mixing and
44
+ scaling color components.\n sass-cmyk outputs CMYK color values using cmyk()
45
+ function syntax supported by AntennaHouse and PrinceXML PDF formatters,\n making
46
+ it a great fit for doing print typesetting with CSS.\n"
47
+ email: sanders@oreilly.com
48
+ executables: []
49
+ extensions: []
50
+ extra_rdoc_files: []
51
+ files:
52
+ - lib/sass-cmyk.rb
53
+ homepage: https://github.com/sandersk/sass-cmyk
54
+ licenses:
55
+ - MIT
56
+ metadata: {}
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: 1.8.7
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 2.2.1
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: A plugin for Sass that provides preprocessing for CMYK colors, including
77
+ some basic color manipulation functions
78
+ test_files: []