|
| 1 | +# Extracting colors from an image |
| 2 | + |
| 3 | +[TOC] |
| 4 | + |
| 5 | +To extract colors from an image that are suitable for generating schemes, use |
| 6 | +the `quantize` and `score` libraries. |
| 7 | + |
| 8 | +TODO: Insert here a diagram for image-to-colors extraction. |
| 9 | + |
| 10 | +## Step 1 — Image to Pixels |
| 11 | + |
| 12 | +First, convert an image into **an array of pixels in ARGB |
| 13 | +format**. MCU does not provide this feature; please use the idiomatic method |
| 14 | +provided by your language. |
| 15 | + |
| 16 | +For example, in Java, one may use the `BufferedImage.getRGB` method: |
| 17 | + |
| 18 | +```java |
| 19 | +import java.awt.image.BufferedImage; |
| 20 | + |
| 21 | +class ImageUtils { |
| 22 | + // ... |
| 23 | + |
| 24 | + public static int[] imageToPixels(BufferedImage image) { |
| 25 | + int width = image.getWidth(); |
| 26 | + int height = image.getHeight(); |
| 27 | + BufferedImage outputImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); |
| 28 | + return image.getRGB(0, 0, width, height, null, 0, width); |
| 29 | + } |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +## Step 2 — Pixels to Prominent Colors |
| 34 | + |
| 35 | +Once you have the array of pixels, pass it into `QuantizerCelebi.quantize` |
| 36 | +provided by the `quantize` library. |
| 37 | + |
| 38 | +<section> |
| 39 | + |
| 40 | +###### Dart |
| 41 | + |
| 42 | +```dart |
| 43 | +final quantizerResult = await QuantizerCelebi.quantize(pixels, maxColors); |
| 44 | +``` |
| 45 | + |
| 46 | +###### Java |
| 47 | + |
| 48 | +```java |
| 49 | +QuantizerResult quantizerResult = QuantizerCelebi.quantize(pixels, maxColors); |
| 50 | +``` |
| 51 | + |
| 52 | +###### TypeScript |
| 53 | + |
| 54 | +```typescript |
| 55 | +const quantizerResult = QuantizerCelebi.quantize(pixels, maxColors); |
| 56 | +``` |
| 57 | + |
| 58 | +###### C++ |
| 59 | + |
| 60 | +```cpp |
| 61 | +QuantizerResult quantizer_result = QuantizeCelebi(pixels, max_colors); |
| 62 | +``` |
| 63 | + |
| 64 | +###### Swift |
| 65 | + |
| 66 | +```swift |
| 67 | +let quantizerResult = QuantizerCelebi().quantize(pixels, maxColors) |
| 68 | +``` |
| 69 | + |
| 70 | +</section> |
| 71 | + |
| 72 | +The parameter `maxColors` is a limit on the number of colors returned by the |
| 73 | +quantizer. A reasonable default is 128. |
| 74 | + |
| 75 | +## Step 3 — Prominent Colors to Suitable Seed Colors |
| 76 | + |
| 77 | +Use the `Score.score` method provided by the `score` library to extract colors |
| 78 | +that are suitable as seeds for color schemes, ranked by decreasing suitability. |
| 79 | + |
| 80 | +<section> |
| 81 | + |
| 82 | +###### Dart |
| 83 | + |
| 84 | +```dart |
| 85 | +final colors = Score.score(quantizerResult.colorToCount); |
| 86 | +``` |
| 87 | + |
| 88 | +###### Java |
| 89 | + |
| 90 | +```java |
| 91 | +List<Integer> colors = Score.score(quantizerResult); |
| 92 | +``` |
| 93 | + |
| 94 | +###### TypeScript |
| 95 | + |
| 96 | +```typescript |
| 97 | +final colors = Score.score(quantizerResult); |
| 98 | +``` |
| 99 | + |
| 100 | +###### C++ |
| 101 | + |
| 102 | +```cpp |
| 103 | +std::vector<Argb> colors = RankedSuggestions(quantizer_result.color_to_count); |
| 104 | +``` |
| 105 | + |
| 106 | +###### Swift |
| 107 | + |
| 108 | +```swift |
| 109 | +let colors = Score.score(quantizerResult.colorToCount) |
| 110 | +``` |
| 111 | + |
| 112 | +</section> |
0 commit comments