Skip to content

Instantly share code, notes, and snippets.

@Just1B
Created January 6, 2022 12:36
Show Gist options
  • Save Just1B/16a5e78e8b519365ae79ca1acca58a9e to your computer and use it in GitHub Desktop.
Save Just1B/16a5e78e8b519365ae79ca1acca58a9e to your computer and use it in GitHub Desktop.

Revisions

  1. Just1B created this gist Jan 6, 2022.
    34 changes: 34 additions & 0 deletions interpolation.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    function ColorToHex(color) {
    const hexadecimal = color.toString(16)

    return hexadecimal.length === 1 ? "0" + hexadecimal : hexadecimal
    }

    const items = [ "cold", "semi-cold", "ok", "hot", "very_hot" ]
    const items_colors = []

    let alpha = 0.0

    const step = 1 / items.length

    items.map( (item) => {

    alpha += step;

    // color_start = "#4cc9f0" -> rgb(76,201,240)
    // color_end = "#f72585" -> rgb(247,37,133)

    // if you have a lot of items -> color palette must have a good diff between channels

    // color_gradient_canal = min_channel_value * alpha + (1 - alpha) * max_channel_value

    const r = parseInt(76 * alpha + (1 - alpha) * 247);
    const g = parseInt(37 * alpha + (1 - alpha) * 201);
    const b = parseInt(133 * alpha + (1 - alpha) * 240);

    const hex = "#" + ColorToHex(r) + ColorToHex(g) + ColorToHex(b)

    items_colors.push({"color" : item, "color_hex": hex})
    })

    console.log(items_colors)