You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP

github.com/cenkalti/dominantcolor

Package Overview
Dependencies
2
File Explorer

Install Socket

Detect and block malicious and high-risk dependencies

# github.com/cenkalti/dominantcolor

Package dominantcolor provides a function for finding a color that represents the calculated dominant color in the image. This uses a KMean clustering algorithm to find clusters of pixel colors in RGB space. The algorithm is ported from Chromium source code: RGB KMean Algorithm (N clusters, M iterations): 1. Pick N starting colors by randomly sampling the pixels. If you see a color you already saw keep sampling. After a certain number of tries just remove the cluster and continue with N = N-1 clusters (for an image with just one color this should devolve to N=1). These colors are the centers of your N clusters. 2. For each pixel in the image find the cluster that it is closest to in RGB space. Add that pixel's color to that cluster (we keep a sum and a count of all of the pixels added to the space, so just add it to the sum and increment count). 3. Calculate the new cluster centroids by getting the average color of all of the pixels in each cluster (dividing the sum by the count). 4. See if the new centroids are the same as the old centroids. a) If this is the case for all N clusters than we have converged and can move on. b) If any centroid moved, repeat step 2 with the new centroids for up to M iterations. 5. Once the clusters have converged or M iterations have been tried, sort the clusters by weight (where weight is the number of pixels that make up this cluster). 6. Going through the sorted list of clusters, pick the first cluster with the largest weight that's centroid falls between |lower_bound| and |upper_bound|. Return that color. If no color fulfills that requirement return the color with the largest weight regardless of whether or not it fulfills the equation above.

Version published

## Dominantcolor

Find dominant color in images

```.css-vlgn5s{display:inline-block;font-family:var(--chakra-fonts-mono);background:var(--badge-bg);box-shadow:var(--badge-shadow);overflow-wrap:anywhere;--badge-bg:var(--chakra-colors-purple-100);--badge-color:var(--chakra-colors-purple-800);background-color:var(--chakra-colors-bg-subtle);border-radius:var(--chakra-radii-md);color:inherit;font-size:0.875em;-webkit-padding-start:var(--chakra-space-1-5);padding-inline-start:var(--chakra-space-1-5);-webkit-padding-end:var(--chakra-space-1-5);padding-inline-end:var(--chakra-space-1-5);white-space:break-spaces;}.chakra-ui-dark .css-vlgn5s:not([data-theme]),[data-theme=dark] .css-vlgn5s:not([data-theme]),.css-vlgn5s[data-theme=dark]{--badge-bg:rgba(214, 188, 250, 0.16);--badge-color:var(--chakra-colors-purple-200);}pre>.css-vlgn5s{background-color:var(--chakra-colors-transparent);border-width:0;border-radius:0px;font-size:inherit;padding:0px;white-space:inherit;}```import "github.com/cenkalti/dominantcolor"
``````

Package dominantcolor provides a function for finding a color that represents the calculated dominant color in the image. This uses a KMean clustering algorithm to find clusters of pixel colors in RGB space.

The algorithm is ported from Chromium source code:

##### Example
``````package main

import (
"fmt"
"github.com/cenkalti/dominantcolor"
"image"
_ "image/jpeg"
_ "image/png"
"os"
)

func FindDomiantColor(fileInput string) (string, error) {
f, err := os.Open(fileInput)
defer f.Close()
if err != nil {
return "", err
}
img, _, err := image.Decode(f)
if err != nil {
return "", err
}

return dominantcolor.Hex(dominantcolor.Find(img)), nil
}

func main() {
fmt.Println(FindDomiantColor("aa.png"))
}

``````
##### Output:
``````#CA5527
``````

Thanks to @stuartmscott for creating a GUI: https://github.com/stuartmscott/dominantcolor

FAQs

Package last updated on 26 Oct 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

## Related posts

Product

• Integrations
• Docs
• Pricing
• FAQ
• Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

• Terms
• Privacy
• Security

Made with ⚡️ by Socket Inc