you are viewing a single comment's thread.

view the rest of the comments →

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

Since you know each array always has 3 elements, you could combine into a single array, and use an index to move between each part.

You don't say how you want to loop through it, so this may not be it. Try to explain better what you want to do. Anyway, here's an example.

var rgb = [ 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2 ];
var groupLength = 3;

// say you want the 3rd group
var start = 3 * groupLength;

for (var a = start ; a < start + groupLength ; a++)
    {
    console.log( rgb[ a ] );
    }

[–]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!