Introduce ImageColors
ClosedPublic

Authored by mart on May 11 2020, 3:06 PM.

Details

Reviewers
cblack
Group Reviewers
Kirigami
Commits
R169:3986ad9d0955: Introduce ImageColors
Summary

ImageColors is a class to extract color statistics out of an image. it supports
as source :

  • Grabbing a static frame out of any Item
  • A QImage or a QIcon (for instance coming from a QAbstractItemModel
  • An icon name from the theme

By a pixel-by pixel analisys it uses a K-means clustering algorythm
https://en.wikipedia.org/wiki/K-means_clustering
that groups averages of colors group that have a computed "distance" less than an
arbitrary value and so extracts a palette of the "dominant" colors of the image,
having also other imformations such as the % occurrence of each cluster, a
complementary color for each cluster, the global average color, the most
"vibrant" color of the image, usable as an accent, and good colors for text and
background, based on the extracted palette

Test Plan



Diff Detail

Repository
R169 Kirigami
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.
mart created this revision.May 11 2020, 3:06 PM
Restricted Application added a project: Kirigami. · View Herald TranscriptMay 11 2020, 3:06 PM
Restricted Application added a subscriber: plasma-devel. · View Herald Transcript
mart requested review of this revision.May 11 2020, 3:06 PM
mart edited the test plan for this revision. (Show Details)May 11 2020, 3:08 PM
cblack added a subscriber: cblack.May 11 2020, 3:16 PM
cblack added inline comments.
tests/imagecolorstest.qml
1 ↗(On Diff #82537)

There's no test cases here, this should probably go in examples.

cblack added inline comments.May 11 2020, 3:56 PM
src/colorutils.h
182
Returns the CIELAB chroma of the given color.

CIELAB chroma may give a better quantification of how vibrant a color is compared to HSV saturation.

\sa https://en.wikipedia.org/wiki/Colorfulness
\sa https://en.wikipedia.org/wiki/CIELAB_color_space
src/imagecolors.h
65–71
The source from which colors should be extracted from.
`source` can be one of the following:
* Item
* QImage
* QIcon
* Icon name

Note that an Item's color palette will only be extracted once unless you call `update()`, regardless of how the item hanges.
76–83
A list of colors and related information about then.

Each list item has the following properties:
* `color`: The color of the list item.
* `ratio`: How dominant the color is in the source image.
* `contrastingColor`: The color from the source image that's closest to the inverse of `color`.

The list is sorted by `ratio`; the first element is the most dominant color in the source image and the last element is the least dominant color of the image.

\note K-means clustering is used to extract these colors; see https://en.wikipedia.org/wiki/K-means_clustering.
87–90

I think it would be better to have this return a ColorUtils::Brightness enum.

93
The average color of the source image.
98
The dominant color of the source image.

The dominant color of the image is the color of the largest cluster in the image.
\sa https://en.wikipedia.org/wiki/K-means_clustering
103
The color closest to the inverse of the dominant color found in the source image.
108–110
An accent color extracted from the source image.

The accent color is the color cluster with the highest CIELAB chroma in the source image.

\sa https://en.wikipedia.org/wiki/Colorfulness#Chroma
115–117
A color suitable for rendering text and other foreground over the source image.

On dark items, this will be the color closest to white in the image if it's light enough, or a bright gray otherwise.
On light items, this will be the color closest to black in the image if it's dark enough, or a dark gray otherwise.
121–125
A color suitable for rendering a background behind the source image.

On dark items, this will be the color closest to black in the image if it's dark enough, or a dark gray otherwise.
On light items, this will be the color closest to white in the image if it's light enough, or a bright gray otherwise.
129
The lightest color of the source image.
134
The darkest color of the source image.
mart updated this revision to Diff 82645.May 12 2020, 9:37 AM
mart marked 12 inline comments as done.
  • adress feedbacks
cblack accepted this revision.May 12 2020, 5:25 PM
This revision is now accepted and ready to land.May 12 2020, 5:25 PM
This revision was automatically updated to reflect the committed changes.