My Math Forum  

Go Back   My Math Forum > College Math Forum > Real Analysis

Real Analysis Real Analysis Math Forum

LinkBack Thread Tools Display Modes
February 28th, 2014, 11:48 AM   #1
Joined: Feb 2014

Posts: 8
Thanks: 0

RGB Colors

Hey guys, I'm not sure if this is the right forum to post in, but I hope you can help me anyway.

For a game I'm working on I'm looking to generate all colors in the RGB spectrum (see the image below).

Just in case you don't know what RGB colors are, here's a brief explanation:
The colors you see on your screen are made up of three base colors, red, green and blue.
The brightness of each color varies between 0 and 255.
By mixing the three colors in various brightness a lot of colors can be achieved.

I want three functions, one for each color: red, green and blue.
The function should take a decimal from 0 to 1 and return a new number between 0 and 255.

I've made this beautiful graph to showcase the returnvalues of each function:

I'd really appreciate some help
willeb96 is offline  
March 1st, 2014, 09:10 AM   #2
Joined: Feb 2014

Posts: 8
Thanks: 0

Re: RGB Colors

I solved this in a somewhat sloppy way, but it works

If you're from the future and have the some problem as I did, here's how I solved it:

I made this function
function rainbow(x) {
	x = x - Math.floor(x);
	if (x >= 0 && x <= (1/6)) {
		return Math.round(x*6*256);
	} else if (x >= (1/6) && x <= (1/6)*3) {
		return 256;
	} else if (x >= (1/6)*3 && x <= (1/6)*4) {
		return 256-Math.round((x-(1/6)*3)*6*256);
	} else {
		return 0;
Then, to get the various colors for a rainbow I used the function like this:
r: rainbow(x+(1/6)*2)
g: rainbow(x)
b: rainbow(x-(1/6)*2)
willeb96 is offline  

  My Math Forum > College Math Forum > Real Analysis

colors, rgb

Thread Tools
Display Modes

Similar Threads
Thread Thread Starter Forum Replies Last Post
A Group Structure for Unlabeled Necklaces 8beads 8 colors? Henriette Abstract Algebra 0 March 15th, 2014 08:57 AM
How many sequences of colors are possible... sivela Math Events 1 February 22nd, 2011 06:41 PM
Probability of seeing k different colors in sample of size m duffy Advanced Statistics 3 December 30th, 2010 09:40 PM

Copyright © 2017 My Math Forum. All rights reserved.