Map value to color rgba

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












I wrote this function to map value to a color



I am looking for some general feedback on how I can improve the efficiency of the function. and if there is better way to do it.



function getColor($value, $values_range, $opacity = '0.7') 

$deltaS = ($values_range['max'] - $values_range['min']) / 2;
$ds = ($value - $values_range['min']) / $deltaS;
$nearest_color_index = floor($ds);
$alpha = $ds - $nearest_color_index;

$r = $g = $b = -1;
$colors = [['r'=>255,'g' => 60,'b' => 40],
['r' => 255,'g' => 247,'b' =>40],
['r' => 12,'g' => 197,'b' => 17]];

if ($nearest_color_index != 2)
$r = round($colors[$nearest_color_index]['r'] + $alpha * ($colors[$nearest_color_index + 1]['r'] - $colors[$nearest_color_index]['r']));
$g = round($colors[$nearest_color_index]['g'] + ($alpha * ($colors[$nearest_color_index + 1]['g'] - $colors[$nearest_color_index]['g'])));
$b = round($colors[$nearest_color_index]['b'] + $alpha * ($colors[$nearest_color_index + 1]['b'] - $colors[$nearest_color_index]['b']));

return "rgba($r,$g,$b,$opacity)";

else
$r = round($colors[$nearest_color_index]['r']);
$g = round($colors[$nearest_color_index]['g']);
$b = round($colors[$nearest_color_index]['b']);
return "rgba($r,$g,$b,$opacity)";









share|improve this question





















  • is this in a class or purely functional ?
    – Dan
    2 days ago










  • @Dan it's a purely function
    – Eslam Ali
    2 days ago

















up vote
0
down vote

favorite












I wrote this function to map value to a color



I am looking for some general feedback on how I can improve the efficiency of the function. and if there is better way to do it.



function getColor($value, $values_range, $opacity = '0.7') 

$deltaS = ($values_range['max'] - $values_range['min']) / 2;
$ds = ($value - $values_range['min']) / $deltaS;
$nearest_color_index = floor($ds);
$alpha = $ds - $nearest_color_index;

$r = $g = $b = -1;
$colors = [['r'=>255,'g' => 60,'b' => 40],
['r' => 255,'g' => 247,'b' =>40],
['r' => 12,'g' => 197,'b' => 17]];

if ($nearest_color_index != 2)
$r = round($colors[$nearest_color_index]['r'] + $alpha * ($colors[$nearest_color_index + 1]['r'] - $colors[$nearest_color_index]['r']));
$g = round($colors[$nearest_color_index]['g'] + ($alpha * ($colors[$nearest_color_index + 1]['g'] - $colors[$nearest_color_index]['g'])));
$b = round($colors[$nearest_color_index]['b'] + $alpha * ($colors[$nearest_color_index + 1]['b'] - $colors[$nearest_color_index]['b']));

return "rgba($r,$g,$b,$opacity)";

else
$r = round($colors[$nearest_color_index]['r']);
$g = round($colors[$nearest_color_index]['g']);
$b = round($colors[$nearest_color_index]['b']);
return "rgba($r,$g,$b,$opacity)";









share|improve this question





















  • is this in a class or purely functional ?
    – Dan
    2 days ago










  • @Dan it's a purely function
    – Eslam Ali
    2 days ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I wrote this function to map value to a color



I am looking for some general feedback on how I can improve the efficiency of the function. and if there is better way to do it.



function getColor($value, $values_range, $opacity = '0.7') 

$deltaS = ($values_range['max'] - $values_range['min']) / 2;
$ds = ($value - $values_range['min']) / $deltaS;
$nearest_color_index = floor($ds);
$alpha = $ds - $nearest_color_index;

$r = $g = $b = -1;
$colors = [['r'=>255,'g' => 60,'b' => 40],
['r' => 255,'g' => 247,'b' =>40],
['r' => 12,'g' => 197,'b' => 17]];

if ($nearest_color_index != 2)
$r = round($colors[$nearest_color_index]['r'] + $alpha * ($colors[$nearest_color_index + 1]['r'] - $colors[$nearest_color_index]['r']));
$g = round($colors[$nearest_color_index]['g'] + ($alpha * ($colors[$nearest_color_index + 1]['g'] - $colors[$nearest_color_index]['g'])));
$b = round($colors[$nearest_color_index]['b'] + $alpha * ($colors[$nearest_color_index + 1]['b'] - $colors[$nearest_color_index]['b']));

return "rgba($r,$g,$b,$opacity)";

else
$r = round($colors[$nearest_color_index]['r']);
$g = round($colors[$nearest_color_index]['g']);
$b = round($colors[$nearest_color_index]['b']);
return "rgba($r,$g,$b,$opacity)";









share|improve this question













I wrote this function to map value to a color



I am looking for some general feedback on how I can improve the efficiency of the function. and if there is better way to do it.



function getColor($value, $values_range, $opacity = '0.7') 

$deltaS = ($values_range['max'] - $values_range['min']) / 2;
$ds = ($value - $values_range['min']) / $deltaS;
$nearest_color_index = floor($ds);
$alpha = $ds - $nearest_color_index;

$r = $g = $b = -1;
$colors = [['r'=>255,'g' => 60,'b' => 40],
['r' => 255,'g' => 247,'b' =>40],
['r' => 12,'g' => 197,'b' => 17]];

if ($nearest_color_index != 2)
$r = round($colors[$nearest_color_index]['r'] + $alpha * ($colors[$nearest_color_index + 1]['r'] - $colors[$nearest_color_index]['r']));
$g = round($colors[$nearest_color_index]['g'] + ($alpha * ($colors[$nearest_color_index + 1]['g'] - $colors[$nearest_color_index]['g'])));
$b = round($colors[$nearest_color_index]['b'] + $alpha * ($colors[$nearest_color_index + 1]['b'] - $colors[$nearest_color_index]['b']));

return "rgba($r,$g,$b,$opacity)";

else
$r = round($colors[$nearest_color_index]['r']);
$g = round($colors[$nearest_color_index]['g']);
$b = round($colors[$nearest_color_index]['b']);
return "rgba($r,$g,$b,$opacity)";











share|improve this question












share|improve this question




share|improve this question








edited Aug 3 at 15:08
























asked Aug 3 at 2:43









Eslam Ali

959




959











  • is this in a class or purely functional ?
    – Dan
    2 days ago










  • @Dan it's a purely function
    – Eslam Ali
    2 days ago

















  • is this in a class or purely functional ?
    – Dan
    2 days ago










  • @Dan it's a purely function
    – Eslam Ali
    2 days ago
















is this in a class or purely functional ?
– Dan
2 days ago




is this in a class or purely functional ?
– Dan
2 days ago












@Dan it's a purely function
– Eslam Ali
2 days ago





@Dan it's a purely function
– Eslam Ali
2 days ago











1 Answer
1






active

oldest

votes

















up vote
1
down vote













Function name



The function name getColor isn't representative of what it does its better of being named createRgbaString (or something along those lines)



Return early



Your script returns in two different places, this should be changed so you exit the function early, it saves an else statement and a level of identing



Variable nameing style



Your variable naming style is not compatible with PSR2 but that isn't the end of the world.



Line length



Your line length exceeds 80 character line length, this is again not the end of the world but will make it hard to read for some people



Repeated calculation



Your repeatedly do the same calculation if the $index != 2 I would move this into a function so if it needs to be updated you only have todo it in one location



How I might write it



I have used terrible variable names, because I don't have the time do work out the complete logic of your script, so this should be unit tested if your going to use it



function getColor($value, $values_range, $opacity = '0.7')

$deltaS = ($values_range['max'] - $values_range['min']) / 2;
$ds = ($value - $values_range['min']) / $deltaS;
$nearest_color_index = floor($ds);
$alpha = $ds - $nearest_color_index;

$r = $g = $b = -1;

$colors = [
['r'=>255,'g' => 60,'b' => 40],
['r' => 255,'g' => 247,'b' =>40],
['r' => 12,'g' => 197,'b' => 17]
];

$rNormal = getColorIndexPlusModifier($nearest_color_index, "r", 0, $colors);
$gNormal = getColorIndexPlusModifier($nearest_color_index, "g", 0, $colors);
$bNormal = getColorIndexPlusModifier($nearest_color_index, "b", 0, $colors);

if ($nearest_color_index == 2)
return "rgba($rNormal,$gNormal,$bNormal,$opacity)";


$rPlusOne = getColorIndexPlusModifier($nearest_color_index, "r", 1, $colors);
$gPlusOne = getColorIndexPlusModifier($nearest_color_index, "g", 1, $colors);
$bPlusOne = getColorIndexPlusModifier($nearest_color_index, "b", 1, $colors);

$r = doCalculation($rNormal, $alpha, $rPlusOne);
$g = doCalculation($gNormal, $alpha, $gPlusOne);
$b = doCalculation($bNormal, $alpha, $bPlusOne);

return "rgba($r,$g,$b,$opacity)";


function getColorIndexPlusModifier($colorIndex, $colorString, $toModifyBy, $colors)

return round($colors[$colorIndex + $toModifyBy][$colorString]);

//TODO Rename to something meaningful
function doCalculation($normalValue, $alpha, $plusOneValue)

return round($normalValue + $alpha * ($plusOneValue - $normalValue));






share|improve this answer





















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f200864%2fmap-value-to-color-rgba%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    Function name



    The function name getColor isn't representative of what it does its better of being named createRgbaString (or something along those lines)



    Return early



    Your script returns in two different places, this should be changed so you exit the function early, it saves an else statement and a level of identing



    Variable nameing style



    Your variable naming style is not compatible with PSR2 but that isn't the end of the world.



    Line length



    Your line length exceeds 80 character line length, this is again not the end of the world but will make it hard to read for some people



    Repeated calculation



    Your repeatedly do the same calculation if the $index != 2 I would move this into a function so if it needs to be updated you only have todo it in one location



    How I might write it



    I have used terrible variable names, because I don't have the time do work out the complete logic of your script, so this should be unit tested if your going to use it



    function getColor($value, $values_range, $opacity = '0.7')

    $deltaS = ($values_range['max'] - $values_range['min']) / 2;
    $ds = ($value - $values_range['min']) / $deltaS;
    $nearest_color_index = floor($ds);
    $alpha = $ds - $nearest_color_index;

    $r = $g = $b = -1;

    $colors = [
    ['r'=>255,'g' => 60,'b' => 40],
    ['r' => 255,'g' => 247,'b' =>40],
    ['r' => 12,'g' => 197,'b' => 17]
    ];

    $rNormal = getColorIndexPlusModifier($nearest_color_index, "r", 0, $colors);
    $gNormal = getColorIndexPlusModifier($nearest_color_index, "g", 0, $colors);
    $bNormal = getColorIndexPlusModifier($nearest_color_index, "b", 0, $colors);

    if ($nearest_color_index == 2)
    return "rgba($rNormal,$gNormal,$bNormal,$opacity)";


    $rPlusOne = getColorIndexPlusModifier($nearest_color_index, "r", 1, $colors);
    $gPlusOne = getColorIndexPlusModifier($nearest_color_index, "g", 1, $colors);
    $bPlusOne = getColorIndexPlusModifier($nearest_color_index, "b", 1, $colors);

    $r = doCalculation($rNormal, $alpha, $rPlusOne);
    $g = doCalculation($gNormal, $alpha, $gPlusOne);
    $b = doCalculation($bNormal, $alpha, $bPlusOne);

    return "rgba($r,$g,$b,$opacity)";


    function getColorIndexPlusModifier($colorIndex, $colorString, $toModifyBy, $colors)

    return round($colors[$colorIndex + $toModifyBy][$colorString]);

    //TODO Rename to something meaningful
    function doCalculation($normalValue, $alpha, $plusOneValue)

    return round($normalValue + $alpha * ($plusOneValue - $normalValue));






    share|improve this answer

























      up vote
      1
      down vote













      Function name



      The function name getColor isn't representative of what it does its better of being named createRgbaString (or something along those lines)



      Return early



      Your script returns in two different places, this should be changed so you exit the function early, it saves an else statement and a level of identing



      Variable nameing style



      Your variable naming style is not compatible with PSR2 but that isn't the end of the world.



      Line length



      Your line length exceeds 80 character line length, this is again not the end of the world but will make it hard to read for some people



      Repeated calculation



      Your repeatedly do the same calculation if the $index != 2 I would move this into a function so if it needs to be updated you only have todo it in one location



      How I might write it



      I have used terrible variable names, because I don't have the time do work out the complete logic of your script, so this should be unit tested if your going to use it



      function getColor($value, $values_range, $opacity = '0.7')

      $deltaS = ($values_range['max'] - $values_range['min']) / 2;
      $ds = ($value - $values_range['min']) / $deltaS;
      $nearest_color_index = floor($ds);
      $alpha = $ds - $nearest_color_index;

      $r = $g = $b = -1;

      $colors = [
      ['r'=>255,'g' => 60,'b' => 40],
      ['r' => 255,'g' => 247,'b' =>40],
      ['r' => 12,'g' => 197,'b' => 17]
      ];

      $rNormal = getColorIndexPlusModifier($nearest_color_index, "r", 0, $colors);
      $gNormal = getColorIndexPlusModifier($nearest_color_index, "g", 0, $colors);
      $bNormal = getColorIndexPlusModifier($nearest_color_index, "b", 0, $colors);

      if ($nearest_color_index == 2)
      return "rgba($rNormal,$gNormal,$bNormal,$opacity)";


      $rPlusOne = getColorIndexPlusModifier($nearest_color_index, "r", 1, $colors);
      $gPlusOne = getColorIndexPlusModifier($nearest_color_index, "g", 1, $colors);
      $bPlusOne = getColorIndexPlusModifier($nearest_color_index, "b", 1, $colors);

      $r = doCalculation($rNormal, $alpha, $rPlusOne);
      $g = doCalculation($gNormal, $alpha, $gPlusOne);
      $b = doCalculation($bNormal, $alpha, $bPlusOne);

      return "rgba($r,$g,$b,$opacity)";


      function getColorIndexPlusModifier($colorIndex, $colorString, $toModifyBy, $colors)

      return round($colors[$colorIndex + $toModifyBy][$colorString]);

      //TODO Rename to something meaningful
      function doCalculation($normalValue, $alpha, $plusOneValue)

      return round($normalValue + $alpha * ($plusOneValue - $normalValue));






      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        Function name



        The function name getColor isn't representative of what it does its better of being named createRgbaString (or something along those lines)



        Return early



        Your script returns in two different places, this should be changed so you exit the function early, it saves an else statement and a level of identing



        Variable nameing style



        Your variable naming style is not compatible with PSR2 but that isn't the end of the world.



        Line length



        Your line length exceeds 80 character line length, this is again not the end of the world but will make it hard to read for some people



        Repeated calculation



        Your repeatedly do the same calculation if the $index != 2 I would move this into a function so if it needs to be updated you only have todo it in one location



        How I might write it



        I have used terrible variable names, because I don't have the time do work out the complete logic of your script, so this should be unit tested if your going to use it



        function getColor($value, $values_range, $opacity = '0.7')

        $deltaS = ($values_range['max'] - $values_range['min']) / 2;
        $ds = ($value - $values_range['min']) / $deltaS;
        $nearest_color_index = floor($ds);
        $alpha = $ds - $nearest_color_index;

        $r = $g = $b = -1;

        $colors = [
        ['r'=>255,'g' => 60,'b' => 40],
        ['r' => 255,'g' => 247,'b' =>40],
        ['r' => 12,'g' => 197,'b' => 17]
        ];

        $rNormal = getColorIndexPlusModifier($nearest_color_index, "r", 0, $colors);
        $gNormal = getColorIndexPlusModifier($nearest_color_index, "g", 0, $colors);
        $bNormal = getColorIndexPlusModifier($nearest_color_index, "b", 0, $colors);

        if ($nearest_color_index == 2)
        return "rgba($rNormal,$gNormal,$bNormal,$opacity)";


        $rPlusOne = getColorIndexPlusModifier($nearest_color_index, "r", 1, $colors);
        $gPlusOne = getColorIndexPlusModifier($nearest_color_index, "g", 1, $colors);
        $bPlusOne = getColorIndexPlusModifier($nearest_color_index, "b", 1, $colors);

        $r = doCalculation($rNormal, $alpha, $rPlusOne);
        $g = doCalculation($gNormal, $alpha, $gPlusOne);
        $b = doCalculation($bNormal, $alpha, $bPlusOne);

        return "rgba($r,$g,$b,$opacity)";


        function getColorIndexPlusModifier($colorIndex, $colorString, $toModifyBy, $colors)

        return round($colors[$colorIndex + $toModifyBy][$colorString]);

        //TODO Rename to something meaningful
        function doCalculation($normalValue, $alpha, $plusOneValue)

        return round($normalValue + $alpha * ($plusOneValue - $normalValue));






        share|improve this answer













        Function name



        The function name getColor isn't representative of what it does its better of being named createRgbaString (or something along those lines)



        Return early



        Your script returns in two different places, this should be changed so you exit the function early, it saves an else statement and a level of identing



        Variable nameing style



        Your variable naming style is not compatible with PSR2 but that isn't the end of the world.



        Line length



        Your line length exceeds 80 character line length, this is again not the end of the world but will make it hard to read for some people



        Repeated calculation



        Your repeatedly do the same calculation if the $index != 2 I would move this into a function so if it needs to be updated you only have todo it in one location



        How I might write it



        I have used terrible variable names, because I don't have the time do work out the complete logic of your script, so this should be unit tested if your going to use it



        function getColor($value, $values_range, $opacity = '0.7')

        $deltaS = ($values_range['max'] - $values_range['min']) / 2;
        $ds = ($value - $values_range['min']) / $deltaS;
        $nearest_color_index = floor($ds);
        $alpha = $ds - $nearest_color_index;

        $r = $g = $b = -1;

        $colors = [
        ['r'=>255,'g' => 60,'b' => 40],
        ['r' => 255,'g' => 247,'b' =>40],
        ['r' => 12,'g' => 197,'b' => 17]
        ];

        $rNormal = getColorIndexPlusModifier($nearest_color_index, "r", 0, $colors);
        $gNormal = getColorIndexPlusModifier($nearest_color_index, "g", 0, $colors);
        $bNormal = getColorIndexPlusModifier($nearest_color_index, "b", 0, $colors);

        if ($nearest_color_index == 2)
        return "rgba($rNormal,$gNormal,$bNormal,$opacity)";


        $rPlusOne = getColorIndexPlusModifier($nearest_color_index, "r", 1, $colors);
        $gPlusOne = getColorIndexPlusModifier($nearest_color_index, "g", 1, $colors);
        $bPlusOne = getColorIndexPlusModifier($nearest_color_index, "b", 1, $colors);

        $r = doCalculation($rNormal, $alpha, $rPlusOne);
        $g = doCalculation($gNormal, $alpha, $gPlusOne);
        $b = doCalculation($bNormal, $alpha, $bPlusOne);

        return "rgba($r,$g,$b,$opacity)";


        function getColorIndexPlusModifier($colorIndex, $colorString, $toModifyBy, $colors)

        return round($colors[$colorIndex + $toModifyBy][$colorString]);

        //TODO Rename to something meaningful
        function doCalculation($normalValue, $alpha, $plusOneValue)

        return round($normalValue + $alpha * ($plusOneValue - $normalValue));







        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered 2 days ago









        Dan

        348211




        348211






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f200864%2fmap-value-to-color-rgba%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Python Lists

            Aion

            JavaScript Array Iteration Methods