red-palette 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f12b7fc4cf789b440ea06399e24819d4579a78ad85d13cd9f39098470ff52c1c
4
- data.tar.gz: 73cce0dfa74982c73d927f548313a46a46310060423cfda3e848b3710c8adb34
3
+ metadata.gz: 3122a06d16fd374d4d82abe074c2dbff00a2b75f546f785c9cc72ded5d0a2148
4
+ data.tar.gz: d4003bbc74cdfdaddf32775b07d2870f1b3f4f3c23da36ec1735a23f007802be
5
5
  SHA512:
6
- metadata.gz: 4b7e09883c5eb529d2c999aca1c297655d6d162fb3f49af18272dc29e947e1573597447f0d09e1e4592c1d02c444fc7c4200335fe814e362167251f2bba4beea
7
- data.tar.gz: 1c017fc86fc8cfc3b178773ae17235e0094d215a382abc61e161fd3223fc9c84dda2caf24cf266b84b824ecfec02070510934a793c42d4bc3a88f9a0620cc7a2
6
+ metadata.gz: 73d4618b458ddfec249662b3b3082ce61a3482671f8f6d26365fe510dc7fd9f3b6e3c78ea7eb9ddacff856184e376aa0957d3d0f4b5f7faa465e1ee584e29fcb
7
+ data.tar.gz: e0c74affc25e2f964c6f076caa739c7431d7b0c6957912198cfe2a930fafabdfc30bce259eda1e4a2333d070f2032f3b912a1ff0da83a6dc817364bd9b57254c
data/lib/palette.rb CHANGED
@@ -70,6 +70,42 @@ class Palette
70
70
  @desaturate_factor = desaturate_factor
71
71
  end
72
72
 
73
+ private def parse_cubehelix_args(str)
74
+ if str.start_with?("ch:")
75
+ str = str[3..-1]
76
+ end
77
+ if str.end_with?("_r")
78
+ reverse = true
79
+ str = str[0..-3]
80
+ else
81
+ reverse = false
82
+ end
83
+ if str.empty?
84
+ return [], {reverse: reverse}
85
+ end
86
+
87
+ short_key_map = {
88
+ "s" => :start, "r" => :rot, "g" => :gamma,
89
+ "h" => :hue, "l" => :light, "d" => :dark
90
+ }
91
+
92
+ all_args = str.split(",")
93
+ args, kwargs = [], {}
94
+ all_args.each do |a|
95
+ if a.include? "="
96
+ key, value = a.split("=")
97
+ key = short_key_map.fetch(key.strip, key).to_sym
98
+ kwargs[key] = Float(value.strip)
99
+ else
100
+ args << Float(a.strip)
101
+ end
102
+ end
103
+
104
+ kwargs[:reverse] = true if reverse
105
+
106
+ return args, kwargs
107
+ end
108
+
73
109
  attr_reader :name, :colors, :desaturate_factor
74
110
 
75
111
  def n_colors
@@ -88,11 +124,15 @@ class Palette
88
124
  end
89
125
 
90
126
  def [](i)
91
- @palette[i % n_colors]
127
+ @colors[i % n_colors]
128
+ end
129
+
130
+ def to_colormap(n=self.n_colors)
131
+ Colors::ListedColormap.new(colors, n_colors: n)
92
132
  end
93
133
 
94
134
  def to_ary
95
- @palette.dup
135
+ @colors.dup
96
136
  end
97
137
 
98
138
  def to_iruby
@@ -53,10 +53,30 @@ class Palette
53
53
  (0...n_colors).map {|i| Colors::HUSL.new(hues[i], s, l) }
54
54
  end
55
55
 
56
- def self.cubehelix_colors(n_colors, start=0, rot=0.4r, gamma=1.0r, hue=0.8r,
57
- light=0.85r, dark=0.15r, reverse=false, as_cmap: false)
58
- raise NotImplementedError,
59
- "Cubehelix palette has not been implemented"
56
+ def self.cubehelix_colors(n_colors=6, start: 0, rot: 0.4r, gamma: 1.0r, hue: 0.8r,
57
+ light: 0.85r, dark: 0.15r, reverse: false)
58
+ get_color_function = ->(p0, p1) do
59
+ color = -> (x) do
60
+ xg = x ** gamma
61
+ a = hue * xg * (1 - xg) / 2
62
+ phi = 2 * Math::PI * (start / 3 + rot * x)
63
+ return xg + a * (p0 * Math.cos(phi) + p1 * Math.sin(phi))
64
+ end
65
+ return color
66
+ end
67
+
68
+ segmented_data = {
69
+ red: get_color_function.(-0.14861, 1.78277),
70
+ green: get_color_function.(-0.29227, -0.90649),
71
+ blue: get_color_function.(1.97294, 0.0),
72
+ }
73
+
74
+ cmap = Colors::LinearSegmentedColormap.new("cubehelix", segmented_data)
75
+
76
+ x = Helper.linspace(light, dark, n_colors)
77
+ pal = cmap[x]
78
+ pal.reverse! if reverse
79
+ pal
60
80
  end
61
81
 
62
82
  def self.matplotlib_colors(name, n_colors=6)
@@ -1,3 +1,3 @@
1
1
  class Palette
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
data/red-palette.gemspec CHANGED
@@ -32,7 +32,7 @@ Gem::Specification.new do |spec|
32
32
  spec.files += Dir.glob("doc/text/*")
33
33
  spec.test_files += Dir.glob("test/**/*")
34
34
 
35
- spec.add_dependency("red-colors")
35
+ spec.add_dependency("red-colors", ">= 0.2.0")
36
36
 
37
37
  spec.add_development_dependency("bundler")
38
38
  spec.add_development_dependency("rake")
data/test/palette_test.rb CHANGED
@@ -122,8 +122,26 @@ class PaletteTest < Test::Unit::TestCase
122
122
  end
123
123
  end
124
124
 
125
- test(".cubehelix_colors") do
126
- omit("Not implemented yet")
125
+ sub_test_case(".cubehelix_colors") do
126
+ data do
127
+ expected = [
128
+ Colors::RGBA.new(0.9312692223325372r, 0.8201921796082118r, 0.7971480974663592r, 1.0r),
129
+ Colors::RGBA.new(0.8559578605899612r, 0.6418993116910497r, 0.6754191211563135r, 1.0r),
130
+ Colors::RGBA.new(0.739734329496642r, 0.4765280683170713r, 0.5959617419736206r, 1.0r),
131
+ Colors::RGBA.new(0.57916573903086r, 0.33934576125314425r, 0.5219003947563425r, 1.0r),
132
+ Colors::RGBA.new(0.37894937987024996r, 0.2224702044652721r, 0.41140014301575434r, 1.0r),
133
+ Colors::RGBA.new(0.1750865648952205r, 0.11840023306916837r, 0.24215989137836502r, 1.0r)
134
+ ]
135
+ expected.map.with_index { |color, i|
136
+ ["colors[#{i}]", {i: i, expected_color: color}]
137
+ }.to_h
138
+ end
139
+ test("default parameters") do |data|
140
+ i, expected_color = data.values_at(:i, :expected_color)
141
+ colors = Palette.cubehelix_colors()
142
+ assert_near(expected_color,
143
+ colors[i])
144
+ end
127
145
  end
128
146
 
129
147
  sub_test_case(".matplotlib_colors") do
@@ -147,4 +165,20 @@ class PaletteTest < Test::Unit::TestCase
147
165
  assert_near(Colors::HSLA.new(120r, 0.8r, 0.5r, 0x99/255r).to_rgba,
148
166
  desaturated_colors[1])
149
167
  end
168
+
169
+ test("to_colormap") do
170
+ cmap = Palette.new(["#ff0000", "#00ff00", "#0000ff"]).to_colormap
171
+ assert_equal([
172
+ Colors::ListedColormap,
173
+ "#ff0000ff",
174
+ "#00ff00ff",
175
+ "#0000ffff"
176
+ ],
177
+ [
178
+ cmap.class,
179
+ cmap[0].to_hex_string,
180
+ cmap[1].to_hex_string,
181
+ cmap[2].to_hex_string
182
+ ])
183
+ end
150
184
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: red-palette
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kenta Murata
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-06-10 00:00:00.000000000 Z
11
+ date: 2021-06-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: red-colors
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: 0.2.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: 0.2.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement