I have a swiftui view that displays a picture which users select from Photo app and shows some texts below the picture. I want to dynamically tune the background color of the view depending on the picture major color. Say, if users select a grass or forest image, the background auto changes to green. Is there any best practice?
picture major color
That's the challenge, defining it.
If I had to do it, I would do the following
- Compute the distribution of colours in the image
- Identify the maximum colour in the distribution
To compute distribution, use CIFilter CIAreaHistogram
and how to use histogram
See how to use a CIFilter (here to compute average colour, which is not what you are looking for) https://www.hackingwithswift.com/example-code/media/how-to-read-the-average-color-of-a-uiimage-using-ciareaaverage