Red Green Blue To Red Yellow Blue – Part 2

Okay, so I kind of forgot to finish up my work on converting a Red Green Blue color system to a Red Yellow Blue color system.  For those of you who don’t remember (and who would) here is the basic recap:

Since the dawn of time, children have been taught their primary colors: Red, Yellow, and Blue.  And they have been taught their secondary colors: Red and Yellow make Orange, Yellow and Blue make Green, Blue and Red make Violet (otherwise known as Purple).  It’s a simple system.  We all know it.  Artists have been using it forever.

The Red Yellow Blue Color Wheel

The Red Yellow Blue Color Wheel

And computers have these lovely computer programs that have all sorts of neat artistic tools.  There’s Adobe Photoshop, GIMP, Paint Shop Pro, even MS Paint.  The list goes on virtually forever.  But they all have one basic flaw:  They don’t use the same color system as artists do.

What?

I know!

Computers base their color system on the invention of the monitor, which itself is based on the color TV.  And these devices base their colors on the light spectrum, which has the primary colors of Red, Green, and Blue.  Making for secondary colors where Red and Green makes Yellow, Green and Blue makes Cyan, and Blue and Red makes Magenta.

The Red Green Blue Color Wheel

The Red Green Blue Color Wheel

Somehow this RGB (or sometimes known as CMY ) color system has dominated computers, so much so that software doesn’t even try to give users an option of a Red Yellow Blue color system.

Until now!

Yes, that’s right.  I, Arah J. Leonard, have written a bit of Python code (though the formulas should translate easily into any language) that converts RGB to RYB and back again.  And let me tell you, it makes a huge difference in calculating complementary colors!  Using this bit of code you can make, for example, button text that always stands out correctly against the color of a button.

And I’m making the code free to use, to all, and free to distribute.  It’s licenced under the LGPL and the MIT License both, giving you the option to use and/or distribute this under either one license (or both) as your project needs.  Use it to your heart’s content folks!  Download your free copy of rgb2ryb.py to convert between Red Green Blue (RGB) and Red Yellow Blue (RYB).

Enjoy!

# Author: Arah J. Leonard
# Copyright 01AUG09
# Distributed under the LGPL - http://www.gnu.org/copyleft/lesser.html
# ALSO distributed under the The MIT License from the Open Source Initiative (OSI) - http://www.opensource.org/licenses/mit-license.php
# You may use EITHER of these licenses to work with / distribute this source code.
# Enjoy!

# Convert a red-green-blue system to a red-yellow-blue system.
def rgb2ryb(r, g, b):
	t = type(r)

	# Remove the whiteness from the color.
	w = float(min(r, g, b))
	r = float(r) - w
	g = float(g) - w
	b = float(b) - w

	mg = max(r, g, b)

	# Get the yellow out of the red+green.
	y = min(r, g)
	r -= y
	g -= y

	# If this unfortunate conversion combines blue and green, then cut each in half to preserve the value's maximum range.
	if b and g:
		b /= 2.0
		g /= 2.0

	# Redistribute the remaining green.
	y += g
	b += g

	# Normalize to values.
	my = max(r, y, b)
	if my:
		n = mg / my
		r *= n
		y *= n
		b *= n

	# Add the white back in.
	r += w
	y += w
	b += w

	# And return back the ryb typed accordingly.
	return t(r), t(y), t(b)

# Convert a red-yellow-blue system to a red-green-blue system.
def ryb2rgb(r, y, b):
	t = type(r)

	# Remove the whiteness from the color.
	w = float(min(r, y, b))
	r = float(r) - w
	y = float(y) - w
	b = float(b) - w

	my = max(r, y, b)

	# Get the green out of the yellow and blue
	g = min(y, b)
	y -= g
	b -= g

	if b and g:
		b *= 2.0
		g *= 2.0

	# Redistribute the remaining yellow.
	r += y
	g += y

	# Normalize to values.
	mg = max(r, g, b)
	if mg:
		n = my / mg
		r *= n
		g *= n
		b *= n

	# Add the white back in.
	r += w
	g += w
	b += w

	# And return back the ryb typed accordingly.
	return t(r), t(g), t(b)

# Return the complementary color values for a given color.  You must also give it the upper limit of the color values, typically 255 for GUIs, 1.0 for OpenGL.
def complimentary(r, g, b, limit=255):
	return limit - r, limit - g, limit - b

# Debugging test code.  Not intended to be used as an application.
if __name__=="__main__":
	red = (255, 0, 0)
	green = (0, 255, 0)
	blue = (0, 0, 255)
	cyan = (0, 255, 255)
	magenta = (255, 0, 255)
	yellow = (255, 255, 0)
	black = (0, 0, 0)
	white = (255, 255, 255)
	orange = (255, 128, 0)
	darkgreen = (0, 128, 0)
	tests = [red, green, blue, cyan, magenta, yellow, black, white, orange, darkgreen, (255, 128, 64), (255, 64, 128), (64, 255, 128), (128, 255, 64), (64, 128, 255), (128, 64, 255)]
	for test in tests:
		ryb = rgb2ryb(test[0], test[1], test[2])
		rgb = ryb2rgb(ryb[0], ryb[1], ryb[2])
		cryb = complimentary(ryb[0], ryb[1], ryb[2])
		crgb = ryb2rgb(cryb[0], cryb[1], cryb[2])
		print test, "rgb2ryb", ryb, "ryb2rgb", rgb
		print "complimentary rgb", complimentary(rgb[0], rgb[1], rgb[2])
		print "complimentary ryb", cryb, "to rgb", crgb
		print

6 Comments

  1. hotspotshield:

    truly loved the article added to my favourites

  2. Kouba:

    I really like your blog and i respect your work. I’ll be a frequent visitor.

  3. Brown:

    Are you a professional journalist? You write very well.

  4. Arah:

    I’ve updated the licensing to be usable/distributable under either the LGPL or the MIT License. (Or both.) Depending on which best suits your project’s needs.

    Sincerely,
    Arah J. Leonard

  5. Joseph Ludovico:

    Sad but true,, I heard this week that criminal gangs are denying our freedoms What next? Letting paedos out of jail? Oops. They do it already!!!

  6. MBT Unono Sandals:

    Could I simply say what a alleviation to search out somebody who actually knows just what they are sharing on the web. You certainly get experience to bring a major issue to light and enable it to be essential. More people need to read this particular and understand it side of the story. I can’t believe you aren’t more popular as you clearly have the gift.

Leave a comment