Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: remove broken dominant border #34

Merged
merged 2 commits into from
Nov 22, 2024

Conversation

knarewski
Copy link
Contributor

@knarewski knarewski commented Nov 22, 2024

Background

I was looking into spec discrepancies between local development and github CI

When running specs on ubuntu 24.04, the "dominant" border rendered using completely different and clearly wrong colour. It turns out that the entire feature is thoroughly broken.

Expected (left) and actual (right) render of dominant coloured border on ubuntu 24.04

Problems

  1. The generated histogram information is plain wrong, suggesting the colours which clearly aren't dominant. These are mostly blueish, with some brownish and two flashy greens. I don't know why that happens, possibly the image preprocessing somehow conflicts with the histogram option.
image
Test image and colours generated using current command on 24.04
("dominant" colour at the bottom)
  1. In the ubuntu 22.04's imagemagick version (used to find dominant colours), the pixel count is not padded, leading to incorrect sorting. That happened to have picked the brown-ish value for border colour in the spec example, which didn't look perfect but also wasn't so clearly wrong. However, ubuntu 24.04's imagemagick adds padding to pixel count, so the sorting is correct, but as per point 1. the entire histogram is wrong. This leads to selecting bright green colour for the border.
image
Test image and colours generated using current command on 22.04
(that one uses wrong sorting, "dominant" colour at the bottom)

Solution

The colorscore gem seems unmaintained, so one alternative is to reimplement dominant handling ourselves. What made the output sane to me was substituting current colorscore's command:

convert spec/fixtures/reference_images/plasma-no-op-output.jpg -resize 400x400 -format %c -dither None -quantize YIQ -colors 16 -depth 8 histogram:info:-

with the following one:

convert spec/fixtures/reference_images/plasma-no-op-output.jpg -resize 400x400 -format %c -dither None -quantize YIQ -colors 16 -depth 8 gif:- \
      | convert - -define histogram:unique-colors=true -format %c histogram:info:- \
      | sort
image
Test image and colours generated using fixed command on 24.04
(fixed sorting and behaviour, "dominant" colour at the bottom)

Ideally natural sorting should be used to handle unpadded output.

That said, I don't expect anyone to miss that feature, so I'm just removing it, leaving the hints about possible fix here in case that's needed :) .

References

original code from colorscore

Appendix

Raw convert output for original command on ubuntu 24.04

# convert spec/fixtures/reference_images/plasma-no-op-output.jpg -resize 400x400 -format %c -dither None -quantize YIQ -colors 16 -depth 8 histogram:info:- | sort
           979: (135,127,127) #877F7F yiq(135,127,127)
          1298: (104,128,127) #68807F yiq(104,128,127)
          3311: (151,127,127) #977F7F yiq(151,127,127)
          6098: (147,127,127) #937F7F yiq(147,127,127)
          6423: (48,87,197) #3057C5 yiq(48,87,197)
          7039: (70,190,195) #46BEC3 yiq(70,190,195)
          7360: (148,127,127) #947F7F yiq(148,127,127)
          7622: (50,66,193) #3242C1 yiq(50,66,193)
          8989: (51,93,176) #335DB0 yiq(51,93,176)
         11721: (57,157,178) #399DB2 yiq(57,157,178)
         12308: (76,162,165) #4CA2A5 yiq(76,162,165)
         12321: (102,127,127) #667F7F yiq(102,127,127)
         12525: (76,107,165) #4C6BA5 yiq(76,107,165)
         13314: (87,217,159) #57D99F yiq(87,217,159)
         18692: (80,248,173) #50F8AD yiq(80,248,173)

Raw convert output for original command on ubuntu 22.04 (note wrong order)

# convert spec/fixtures/reference_images/plasma-no-op-output.jpg -resize 400x400 -format %c -dither None -quantize YIQ -colors 16 -depth 8 histogram:info:- | sort
    11721: (57,157,178) #399DB2 yiq(57,157,178)
    12308: (76,162,165) #4CA2A5 yiq(76,162,165)
    12321: (102,127,127) #667F7F yiq(102,127,127)
    12525: (76,107,165) #4C6BA5 yiq(76,107,165)
    1298: (104,128,127) #68807F yiq(104,128,127)
    13314: (87,217,159) #57D99F yiq(87,217,159)
    18692: (80,248,173) #50F8AD yiq(80,248,173)
    3311: (151,127,127) #977F7F yiq(151,127,127)
    6098: (147,127,127) #937F7F yiq(147,127,127)
    6423: (48,87,197) #3057C5 yiq(48,87,197)
    7039: (70,190,195) #46BEC3 yiq(70,190,195)
    7360: (148,127,127) #947F7F yiq(148,127,127)
    7622: (50,66,193) #3242C1 yiq(50,66,193)
    8989: (51,93,176) #335DB0 yiq(51,93,176)
    979: (135,127,127) #877F7F yiq(135,127,127)

When running specs on ubuntu 24.04, the "dominant" border rendered using completely different
and clearly wrong colour. It turns out that this feature is thoroughly broken.

Problems:
1. The generated histogram information is plain wrong, suggesting the colours which clearly aren't dominant.
   These are mostly blueish, with some brownish and two flashy greens. I don't know why that happens, possibly
   the image preprocessing somehow conflicts with the histogram option.
2. In the ubuntu 22.04's imagemagick version (used to find dominant colours), the pixel count is not padded, leading
   to incorrect sorting. That happened to have picked the brown-ish value for border colour in the spec example,
   which didn't look perfect but also wasn't so clearly wrong.
   However, ubuntu 24.04's imagemagick adds padding to pixel count, so the sorting is correct, but as per point 1.
   the entire histogram is wrong. This leads to selecting bright green colour for the border.

Solution:
The colorscore gem seems unmaintained, so one alternative is to reimplement dominant handling ourselves. What made the output
sane to me was substituting colorscore's command:

convert spec/fixtures/reference_images/plasma-no-op-output.jpg -resize 400x400 -format %c -dither None -quantize YIQ -colors 16 -depth 8 histogram:info:-

with the following one:

convert spec/fixtures/reference_images/plasma-no-op-output.jpg -resize 400x400 -format %c -dither None -quantize YIQ -colors 16 -depth 8 gif:- \
  | convert - -define histogram:unique-colors=true -format %c histogram:info:- \
  | sort

Ideally the natural sorting should be used to handle unpadded output.

That said, I don't expect anyone to miss that feature, so I'm just removing it, leaving the hints to fix it here :) .

References:
- original code from colorscore: https://github.com/quadule/colorscore/blob/32dad8ee5d7500f01208bc9262e8d4d4f3248642/lib/colorscore/histogram.rb#L6
@knarewski knarewski requested a review from a team November 22, 2024 13:17
@knarewski knarewski merged commit a79d930 into master Nov 22, 2024
15 checks passed
@knarewski knarewski deleted the fix-remove-broken-dominant-border branch November 22, 2024 13:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants