Add ColorUtils
Needs ReviewPublic

Authored by cblack on Thu, Mar 19, 4:40 AM.

Details

Reviewers
mart
davidedmundson
Group Reviewers
Plasma
Summary

ColorUtils is a class that offers utilities for working with items and obtaining colours meaningful for UI usage, offering the following items as a start:

  • QColor averageColorForItem(QQuickItem*, int) - Obtains the average colour of an item, ignoring transparent pixels
  • Brightness brightnessForItem(QQuickItem*) - Obtains the brightness of an item
  • QColor alphaBlend(QColor foreground, QColor background) - Gets the colour of overlaying a transparent foreground on a background
  • QColor linearInterpolation(QColor, QColor, double) - Linearly interpolates between two colours
  • QColor adjustColor(QColor, adjustments) - Adjusts the color by fixed values
  • QColor scaleColor(QColor, adjustments) - Scales the color's values

Diff Detail

Repository
R169 Kirigami
Branch
cblack/colour-utils
Lint
No Linters Available
Unit
No Unit Test Coverage
Build Status
Buildable 24243
Build 24261: arc lint + arc unit
cblack created this revision.Thu, Mar 19, 4:40 AM
Restricted Application added a project: Kirigami. · View Herald TranscriptThu, Mar 19, 4:40 AM
Restricted Application added a subscriber: plasma-devel. · View Herald Transcript
cblack requested review of this revision.Thu, Mar 19, 4:40 AM
cblack updated this revision to Diff 77972.Thu, Mar 19, 4:41 AM

Add EOF newlines

cblack updated this revision to Diff 77973.Thu, Mar 19, 4:43 AM

Use words instead of single letters

cblack updated this revision to Diff 77974.Thu, Mar 19, 4:54 AM

Add comments to code

cblack edited the summary of this revision. (Show Details)Thu, Mar 19, 4:57 AM
cblack updated this revision to Diff 77975.Thu, Mar 19, 4:58 AM

Better class description

cblack updated this revision to Diff 77976.Thu, Mar 19, 5:05 AM

Improve documentation comments

mart added a comment.Thu, Mar 19, 9:05 AM

First of all.. I love the idea! :)

I wanted something like that, tough i had a slightly different approach.. i find the global average color usually producing a kinda washed down color (from what happens in the plasma mobile app startup screen)

For another project i needed something along the lines, and ended up using a variant of the k-means clustering technique, so you have averages of the colors grouped in clusters by frequency which can generate interesting palettes to play with (then is trivial to add a global average if really needed).
https://towardsdatascience.com/extracting-colours-from-an-image-using-k-means-clustering-9616348712be

the first implementation is there (i want a way better api for it to be integrated in kirigami tough)
https://invent.kde.org/kde/plasma-bigscreen/-/blob/master/components/imagepalette.h

src/colorutils.cpp
27

sync calls should be avoided whenever possible

cblack updated this revision to Diff 78030.Thu, Mar 19, 5:25 PM

Asynchronous return values

cblack marked an inline comment as done.Thu, Mar 19, 5:26 PM
cblack updated this revision to Diff 78069.Fri, Mar 20, 5:42 AM

Add linear interpolation, alpha blending, colour adjustment, and colour scaling

cblack edited the summary of this revision. (Show Details)Fri, Mar 20, 5:44 AM
mart requested changes to this revision.EditedFri, Mar 20, 10:29 AM

I'm ok for functions that manipulate or give info for colors, but
please no item grabbing in the singlethon. item grabbing is *not* stateless

This revision now requires changes to proceed.Fri, Mar 20, 10:29 AM
cblack updated this revision to Diff 78187.Sat, Mar 21, 6:51 PM

Make brightnessForItem and averageColorForItem take QVariants

cblack updated this revision to Diff 78188.Sat, Mar 21, 6:55 PM

Avoid leaving unused connections; effectively removing leaking state related to item grabbing

davidedmundson requested changes to this revision.Sat, Mar 21, 7:16 PM
davidedmundson added a subscriber: davidedmundson.

What's your intended usecase?

Is Kirigami the right place? The original scope for that was "core application building blocks".

src/colorutils.cpp
79

Depending on use case, it might be better to take a QSGTextureProvider

then images can give you a surface directly without the blit, and you can still take composite items through a ShaderEffectSource

src/pendingvalue.h
26

Having a nested event loop called from QML code is really really dangerous.

If you want it for unit tests or something, fine.

Invokable, absolutely not.

This revision now requires changes to proceed.Sat, Mar 21, 7:16 PM

What's your intended usecase?

Generating UI colours from images, icons, etc. and then applying transformations on them to make them more usable.

Examples:

  • PlaMo's splash screen uses something like this to generate a splash colour background
  • Plasma BigScreen cards also uses something like this to generate card backgrounds
  • Ikona could use this to determine what background and foreground to use for displaying icons from various icon themes at sufficient contrast
cblack updated this revision to Diff 78510.Thu, Mar 26, 12:58 AM

Fix errors