# Color Range: pick color in range base on values in a vector

**URL:** <https://discourse.julialang.org/t/color-range-pick-color-in-range-base-on-values-in-a-vector/80533>\
**Category:** New to Julia\
**Tags:** colors\
**Created:** [May 5, 2022, 11:17am UTC](https://discourse.julialang.org/t/color-range-pick-color-in-range-base-on-values-in-a-vector/80533 "2022-05-05T11:17:11Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 5, 2022, 1:45pm UTC](https://discourse.julialang.org/t/color-range-pick-color-in-range-base-on-values-in-a-vector/80533/4 "2022-05-05T13:45:26Z")

</div>

> [@ffevotte](#):
>
> the linear interpolation that you need

Note that linear interpolation will yield a colormap that is not perceptually uniform, so it will tend to distort data (see e.g. this [nice video on the design of matplotlib’s color schemes](https://www.youtube.com/watch?v=xAoljeRJ3lU)). You can use the [PerceptualColormaps.jl package](https://github.com/peterkovesi/PerceptualColourMaps.jl) to correct this, for example:

```julia
using Colors, ColorSchemes, PerceptualColourMaps

cs = ColorScheme([colorant"yellow", colorant"red"])
origcolors = get(cs, 0:0.01:1) # linear interpolation, not perceptually uniform

# generate corrected colormap:
rgbdata = equalisecolourmap("RGB", RGBA{Float64}.(origcolors), "CIEDE2000", [1,0,0])
newcolors = [RGB(rgb...) for rgb in eachrow(rgb)]

```

Compare the original linear colormap `origcolors` (top) with the corrected colormap `newcolors` (bottom):

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/e/dec9b034ab6f895fb931ccb559df698009b9c2ad.png)  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/0/d0b263fd1ac42cfc8ea7b5d1ab462fdca99a32ab.png)  
You can see that the original linearly interpolated scheme washes out some of the visual contrast, especially on the red (right) side of the scale.

Then you can create a `ColorScheme` object and do interpolation on it:

```julia
newcs = ColorScheme(newcolors)

wealth = [1, 12, 45, 2, 129, 10]
colors = get(newcs, wealth, :extrema)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/7/27c01cb0a17f0467620cd99a1fdd252715b1b0e5.png)  
Note that there is no need to do broadcasting: you can do `get(newcs, somearray, :extrema)` rather than `get.(Ref(newcs), normalized_somearray)`, because the ColorSchemes.jl package implements a vectorized `get` method, and by passing `:extrema` as the third argument it will automatically normalize the data (and you can also pass a different normalization).

---

_[View the full topic](https://discourse.julialang.org/t/color-range-pick-color-in-range-base-on-values-in-a-vector/80533)._
