# Color information and analysis
Imager X comes with a bunch of methods for working with colors.
For starters you can use craft.imager.getDominantColor
and craft.imager.getColorPalette
to get color information from an image.
{% set dominantColor = craft.imager.getDominantColor(image) %}
{% set palette = craft.imager.getColorPalette(image) %}
<p>
Look at that dominant color:
<span style="background: {{ dominantColor }}; width: 20px; height: 20px; display: inline-block;"></span>
</p>
<p>
And look at this color palette:
{% for color in palette %}
<span style="background: {{ color }}; width: 20px; height: 20px; display: inline-block;"></span>
{% endfor %}
</p>
If you need to convert between RGB and HEX color values, there's
craft.imager.hex2rgb
and craft.imager.rgb2hex
.
{% set rgb = craft.imagerx.hex2rgb('#f9c344') %}
Color in RGB is: {{ 'rgb(' ~ rgb[0] ~ ',' ~ rgb[1] ~ ',' ~ rgb[2] ~ ')' }}
{% set hex = craft.imagerx.rgb2hex([249, 195, 68]) %}
Color in hex is: {{ hex }}
Imager also comes with a ton of different helper methods for analyzing
colors. There's nifty stuff like craft.imager.getBrightness
,
craft.imager.getPercievedBrightness
, craft.imager.getContrastRatio
,
and many more. Together
with craft.imager.getDominantColor
these can be used to analyze the
color of your image, and adapt your design based on it (think, light text
on dark images and vice versa).
# When in Imgix...
Imgix also provides functionality to extract color palettes (opens new window),
which is exposed through the ImgixTransformedImageModel
(opens new window).
Here's an example that gets the eigth most dominant colors from a transformed image in json format, and makes a pretty palette:
{% set transformedImage = craft.imager.transformImage(image, { width: 300, height: 400 }) %}
{% set palette = transformedImage.getPalette('json', 8) %}
<div>
{% for paletteColor in palette.colors %}
<span style="display: inline-block; width: 30px; height: 30px; background: {{ paletteColor.hex }};"></span>
{% endfor %}
</div>
Refer to the documentation on ImgixTransformedImageModel
(opens new window)
for more details.