From 7cdb9ece66d8c65764f8ffb32ccfe2eadd138f98 Mon Sep 17 00:00:00 2001 From: mrloop Date: Tue, 3 Jan 2017 10:31:27 +0000 Subject: [PATCH] Color gem expects 4bit or 8bit color strings The color gem expects hex color string with 3 or 6 digits, see: https://github.com/halostatue/color/blob/2159ca233851ca3304414b831d9da368dcbafb04/lib/color/rgb.rb#L609 When miro generates a histogram it uses `Color::RGB.from_html` however image magick can generate 16bit color strings. Using these and `from_html` raises an error. The PR tell image magick we want 8bit color strings. Had been experiencing this issue with Version: ImageMagick 6.8.9-9 Q16 x86_64 2016-11-29 http://www.imagemagick.org --- lib/miro/dominant_colors.rb | 2 +- spec/data/test_16_bit.png | Bin 0 -> 439 bytes spec/miro/dominant_colors_spec.rb | 133 ++++++++++++++++-------------- 3 files changed, 70 insertions(+), 65 deletions(-) create mode 100644 spec/data/test_16_bit.png diff --git a/lib/miro/dominant_colors.rb b/lib/miro/dominant_colors.rb index 8a819ae..d6a7726 100644 --- a/lib/miro/dominant_colors.rb +++ b/lib/miro/dominant_colors.rb @@ -113,7 +113,7 @@ def open_downsampled_image def image_magick_params if Miro.histogram? - "':in[0]' -resize :resolution -colors :colors -colorspace :quantize -quantize :quantize -alpha remove -format %c histogram:info:" + "':in[0]' -resize :resolution -colors :colors -colorspace :quantize -quantize :quantize -depth 8 -alpha remove -format %c histogram:info:" else "':in[0]' -resize :resolution -colors :colors -colorspace :quantize -quantize :quantize :out" end diff --git a/spec/data/test_16_bit.png b/spec/data/test_16_bit.png new file mode 100644 index 0000000000000000000000000000000000000000..eddbf66be7d8988d38bcb331d81be2a0dfdc5d46 GIT binary patch literal 439 zcmeAS@N?(olHy`uVBq!ia0y~yV3Gl{xdfPiq=L+u<3Nff-O<;Pfnj4m_n$;okZ`g` zkS_y6l^O#>Lkk1LFQ8Dv3kHT#0|tgy2@DKYGZ+}e3+C(!v;j)6l?3?(hk?QCIk)OT zB1zuvF8{%RGvfOlpa^GyM`SSr1Gg{;GcwGYBLNg-FY)wsWq-)X%x%c z#WAGf*4yhFc@G=#xH=j}SMaS*Si3;hv^ng9pWpnTHeI$IuQ~sHbIo46HEh|Jd)4oY zw+jji7H==_F+3*m{{P;`7FlaczjpmEoVQy1!<3GoZ?#{!PCVGPE%z&1Lwao(KhTmN zsv$t{l(YatWI|s6BLl-FK`jOb2P-F_&pq5%09Cx?5Cw*T>_T7&E&0sK?IqwO%*A*B zD4|;78c~vxSdwa$T$Bo=7>o=I&29 YnO2EgL)fGVkw6U$p00i_>zopr0F;-7!~g&Q literal 0 HcmV?d00001 diff --git a/spec/miro/dominant_colors_spec.rb b/spec/miro/dominant_colors_spec.rb index 79a26f4..10bb779 100644 --- a/spec/miro/dominant_colors_spec.rb +++ b/spec/miro/dominant_colors_spec.rb @@ -159,86 +159,91 @@ end end + context "histogram method" do - let(:subject) { Miro::DominantColors.new(File.expand_path('spec/data/test.png')) } - let(:hex_colors){["#00ff02","#0000ff","#ff009a","#fa0000","#878787","#585858","#001a6b","#8f0074"]} + let(:hex_colors){["#00ff02","#0000ff","#ff009a","#fa0000","#868787","#585858","#001a6b","#8f0074"]} let(:object_colors){ hex_colors.map{|c| Color::RGB.from_html(c) } } - before do - Miro.stub(:histogram?).and_return(true) - end + %w(spec/data/test.png spec/data/test_16_bit.png).each do |file_path| + context file_path do + let(:subject) { Miro::DominantColors.new(File.expand_path(file_path)) } + before do + Miro.stub(:histogram?).and_return(true) + end - describe "#downsample_and_histogram" do - it "should return an array" do - subject.send(:downsample_and_histogram).should be_an_instance_of(Array) - end + describe "#downsample_and_histogram" do + it "should return an array" do + subject.send(:downsample_and_histogram).should be_an_instance_of(Array) + end + + it "should contain colors" do + subject.send(:downsample_and_histogram).each do |item| + item[1].should be_an_instance_of(Color::RGB) + end + end - it "should contain colors" do - subject.send(:downsample_and_histogram).each do |item| - item[1].should be_an_instance_of(Color::RGB) + it "should have the max length of the color config" do + subject.send(:downsample_and_histogram).count.should <= Miro.options[:color_count] + end end - end - it "should have the max length of the color config" do - subject.send(:downsample_and_histogram).count.should <= Miro.options[:color_count] - end - end + describe "#histogram" do + it "should return a hash" do + subject.histogram.should be_an_instance_of(Array) + end - describe "#histogram" do - it "should return a hash" do - subject.histogram.should be_an_instance_of(Array) - end + it "should start with the most used color" do + subject.histogram.first[1].should == Color::RGB.from_html('#00FF02') + end - it "should start with the most used color" do - subject.histogram.first[1].should == Color::RGB.from_html('#00FF02') - end + it "should end with the less used color" do + subject.histogram.last[1].should == Color::RGB.from_html('#8F0074') + end + end - it "should end with the less used color" do - subject.histogram.last[1].should == Color::RGB.from_html('#8F0074') - end - end + describe "#to_hex" do + it "should be #00FF02 at the first element" do + subject.to_hex.first.should == hex_colors.first + end + it "should be #8F0074 at the first element" do + subject.to_hex.last.should == hex_colors.last + end + it "should have the right values" do + subject.to_hex.should == hex_colors + end + end - describe "#to_hex" do - it "should be #00FF02 at the first element" do - subject.to_hex.first.should == hex_colors.first - end - it "should be #8F0074 at the first element" do - subject.to_hex.last.should == hex_colors.last - end - it "should have the right values" do - subject.to_hex.should == hex_colors - end - end + describe "#to_rgb" do + it "should have the right values" do + subject.to_rgb.should == object_colors.map(&:to_rgb).map(&:to_a) + end + end - describe "#to_rgb" do - it "should have the right values" do - subject.to_rgb.should == object_colors.map(&:to_rgb).map(&:to_a) - end - end + describe "#to_rgba" do + it "should have the right values" do + subject.to_rgba.should == object_colors.map(&:css_rgba) + end + end - describe "#to_rgba" do - it "should have the right values" do - subject.to_rgba.should == object_colors.map(&:css_rgba) - end - end + describe "#to_hsl" do + it "should have the right values" do + subject.to_hsl.should == object_colors.map(&:to_hsl).map(&:to_a) + end + end - describe "#to_hsl" do - it "should have the right values" do - subject.to_hsl.should == object_colors.map(&:to_hsl).map(&:to_a) - end - end + describe "#to_cmyk" do + it "should have the right values" do + subject.to_cmyk.should == object_colors.map(&:to_cmyk).map(&:to_a) + end + end - describe "#to_cmyk" do - it "should have the right values" do - subject.to_cmyk.should == object_colors.map(&:to_cmyk).map(&:to_a) - end - end + describe "#to_yiq" do + it "should have the right values" do + subject.to_yiq.should == object_colors.map(&:to_yiq).map(&:to_a) + end + end - describe "#to_yiq" do - it "should have the right values" do - subject.to_yiq.should == object_colors.map(&:to_yiq).map(&:to_a) + end end end - - end end