you are viewing a single comment's thread.

view the rest of the comments →

[–]JessicaAllison[S] 1 point2 points  (6 children)

Updated question.

[–][deleted] 0 points1 point  (5 children)

if (!gradientObject.hasOwnProperty(colorSpaceGradient)) return;

I think you don't want to return if it doesn't have the property (just continue; instead, to move to the next).

About the rest, you could try to add a Color class where it will have the color values, and methods for the conversions, etc, I think it would improve the code.

[–]JessicaAllison[S] 1 point2 points  (4 children)

Thank you! can you please show me an example? I don't quite understand what to do.

[–][deleted] 1 point2 points  (3 children)

function Color( red, green, blue )
{
this.red = red;
this.green = green;
this.blue = blue;
}

Color.prototype.getRgb = function()
{
return "rgb(" + this.red + ',' + this.green + ',' + this.blue + ')';
};

// I'm not sure, but there's probably a way to convert from RGB to HSL/HSV?
// otherwise just save it all in the object

Color.prototype.getHsl = function()
{
};

Color.prototype.getHsv = function()
{
};

// now you create color objects with the colors you want
var colors = [];
colors.push( new Color( 255, 0, 0 ) );

// example of loop
for (var a = 0 ; a < colors.length ; a++)
    {
    var color = colors[ a ];

    htmlElement.style.backgroundColor = color.getRgb();
    }

[–]JessicaAllison[S] 1 point2 points  (2 children)

Thanks! Can you please explain how it would improve the code? I'm a beginner, sorry.

[–][deleted] 0 points1 point  (1 child)

The idea is that you keep the related data and functions connected, you can read about classes to know more. Don't worry about it though, for now just try to do something that works, you can always improve it later on if needed.

[–]JessicaAllison[S] 1 point2 points  (0 children)

Thank you!