Get stroke value in p5.js

My code is something like this:

stroke(random([0,255]),random([0,255]),random([0,255]));

Now, what I want is to eliminate the possibility of getting stroke(0,0,0) here. I know I can do this with a condition, something like:

if (Stroke.value() == [0,0,0]){...}

but that doesn't exist.

Can someone tell me the correct way to do this?

Tagged:

Answers

  • const c = '#' + hex(~~random(1, 0x1000), 3);
    stroke(c);
    print(c);
    
  • Instead of random([0,255] do random([1,255].

    Kf

  • @kfrajer

    [1,1,1] on a black background will look pretty terrible.

  • @GoToLoop That's not what I asked...

    Elaboration of question:

    stroke(random([0,255]),random([0,255]),random([0,255]));
    

    this will have 8 possible outcomes: red, green, blue, yellow, blue-green, violet, white and black.

    What I want to do, is get only the other 7 colours as the output.

  • edited July 2019 Answer ✓

    https://OpenProcessing.org/sketch/427034

    /**
     * Random 7 Colors (v1.1.2)
     * GoToLoop (2017-May-09)
     *
     * forum.Processing.org/two/discussion/22482/get-stroke-value-in-p5-js#Item_5
     * OpenProcessing.org/sketch/427034
     */
    
    "use strict";
    
    const c = Array(3).fill();
    
    function setup() {
      createCanvas(400, 300);
      frameRate(1);
      colorMode(RGB);
    }
    
    function draw() {
      random7Colors();
      background(c);
      print(c);
    }
    
    function random7Colors(sum) {
      do for (let i = sum = 0; i < 3; sum += c[i++] = (random(1) > .5) * 0xff);
      while (!sum);
    }
    
  • Got the idea. Thanks!

Sign In or Register to comment.