简体   繁体   English

javascript中的R,G,B

[英]R,G,B in javascript

How do you output every rgb color with javascript at the same time easily and quickly ?您如何轻松快速地同时使用 javascript 输出每种 rgb 颜色? I have tried loops and nested loops but they were not what I needed.我尝试过循环和嵌套循环,但它们不是我所需要的。

var r, g, b;
for (r = o,r < 255,r++)
    console.log(r);

for (g = 0,g < 255,g++)
    console.log(g);

for (b = 0,b < 255,b++)
    console.log(b);

There are 256*256*256 or 16 million combinations in the 24 bit R,G,B color space.在 24 位 R、G、B 色彩空间中有 256*256*256 或 1600 万种组合。 A Simple loop over all of them would be:对所有这些的简单循环是:

for(var r = 0; r < 256; r++) {
    for(var g = 0; g < 256; g++){
        for(var b = 0; b < 256; b++){
            console.log("("+r+","+g+","+b+")")
        }
    }
}

This will not be fast and at some point the browser will stop printing to the console.这不会很快,并且在某些时候浏览器将停止打印到控制台。

First correction: Do a triple nested for loop so you can get all the combinations.第一个更正:执行三重嵌套for loop以便您可以获得所有组合。 (256 * 256 * 256) (256*256*256)

Second correction: semicolons instead of commas inside for loop brackets.第二次更正: for loop括号内for loop分号代替逗号。 for (___; ____; ___) . for (___; ____; ___)

Third correction The upper bound value of each for loop should be 256 instead of 255 .第三次修正每个for loop的上限值应该是256而不是255 That's because when you use < 255 , the variable will never take value equal to 255 , instead it will take maximum value 254. Therefore, use 256 to make it go upto 255 .那是因为当您使用< 255 ,变量的值永远不会等于255 ,而是取最大值 254。因此,使用256使其上升到255 (Maximum intensity count of each color channel). (每个颜色通道的最大强度计数)。

var r, g, b;
for(r = 0; r < 256; r++) {
    for(g = 0; g < 256; g++) {
        for(b = 0; b < 256; b++) {
            console.log(r + " " + g + " " + b);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM