Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

78
Views
How to go through two colours with sine wave in p5.js

I know how to do it with black and white

let a = 0;

function setup() {
  createCanvas(400, 400);
}

function draw() {
  background(220);
  fill(map(sin(a), -1, 1, 0, 255));
  rect(20, 20, 50);
  a += 0.01;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.1/p5.js"></script>

example

I would like it to be purple instead of black but I can't do it. I would like to achieve something like this.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I dont know much about p5.js. But simple googling and would offer this solution

let a = 0;
let white;
let purple;

function setup() {
  createCanvas(400, 400);
  white = color(255, 255, 255);
  purple = color(160, 32, 240);
}

function draw() {
  background(220);
  const temp = map(sin(a), -1, 1, 0, 1);
  fill(lerpColor(white, purple, temp));
  rect(20, 20, 50);
  a += 0.01;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.1/p5.js"></script>

With lerp color you could create implementation that supports even more colors.

let white = color(255, 255, 255); // at 0 (-1 on sin)
let purple = color(160, 32, 240); // at 0.5 ( 0 on sin)
let blue = color(0,0,255);  // at 1 (1 on sin)

let temp = map(sin(a),-1,1,0,1);
if(temp < 0.5){
   let temp2 = map(temp, 0, 0.5, 0, 1);
   result = lerpColor(white, purple, temp2);
} else {
   let temp2 = map(temp, 0.5, 1, 0, 1);
   result = lerpColor(purple, blue, temp2);
}

With some refactoring and more work this could support arbitrary number of colors. You could have a sin go through the entire rainbow.

about 4 years ago · Juan Pablo Isaza Report

0

One approach is colorMode(HSB). Hue Saturation Brightness makes it easy to pick a purple hue (280 out of 360), then shift saturation and brightness to fade between grayscale and hue. Saturation and brightness range from 0 to 100.

In this case, I'm fading between 0 and 40 on saturation, and 60 and 100 on brightness. At one extreme, HSB of (280, 0, 100) is pure white. At the other extreme, (280, 40, 60) is purple.

let a = 0;

function setup() {
  createCanvas(400, 400);
  colorMode(HSB);
}

function draw() {
  background(220);
  const v = map(sin(a), -1, 1, 0, 40);
  fill(280, v, 100 - v);
  rect(20, 20, 50);
  a += 0.01;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.1/p5.js"></script>

lerpColor, mentioned in this answer, is probably best for this case, but I'll leave my answer up since it offers some tweakability that lerpColor might not.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!