Posts tagged ‘photoshop’

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

Pay No Attention To That Man Behind The Curtain – Apple Macintosh Trojan Botnet

Apple Macintosh computers have long gone unnoticed in the computer security world.  Mainly because hackers have rarely targeted them.  Why target a system with such a small market share when there’s Windows to exploit?  But lately the Mac is making headway in the war against Windows, and black hats have taken notice.

Bundled in with illegal downloads of things like Apple’s iWork 09 productivity suite and Adobe’s Photoshop CS4 from warez websites have come something new: A Macintosh-specific trojan horse virus.  And not just any virus, but a complex and elegant botnet.  Otherwise known as OSX.Trojan.iServices.A and OSX.Trojan.iServices.B, just iServices.A and iServices.B, or just generally grouped as OSX.Iservice, this botnet trojan is no simple bang-out from your average script kiddie.  It has its own peer-to-peer (p2p) engine, it has startup and encryption services, and it is designed around a highly adaptable structure.  In other words, it’s everything a dangerous virus should be.  And the botnet of zombie-Macs infested with this virus is already launching Denial of Service (DoS) attacks, just like you’d expect from a PC.

The code indicates that, wherever possible, the author tried to use the most flexible and extendible approach when creating it – and therefore we would not be surprised to see a new, modified variant in the near future,” say virus researchers Mario Ballano Barcena and Alfredo Pesoli.  To which they add, “With malware authors showing an increasing interest in the Mac platform, we believe that more advanced [user interface] spoofing tricks may be seen in the future.

I’ve been warning about it for a long time.  As Macs regain popularity (especially in light of the world disgruntled with Windows Vista) the “safety” of the Apple Macintosh is a thing of the past.  The only reason Macs traditionally haven’t been riddled with viruses like PCs have is because Macs are a niche market.  But as their market share grows, so too does the big bullseye on them.  We’ve been seeing more viruses hitting Macintoshes lately, and now we’ve seen the first true in-the-field Mac zombie botnet.  Mac security may never be the same again.  The curtain has been pulled, and we’ve found The Wizard to be nothing more than the same hackable software of everyone else.

Red-Green-Blue To Red-Yellow-Blue – Part 1

I already solved this once, a long while back.  And it was handy code to have.  But unfortunately I lost the code.  So now I’m re-solving the problem.  How do you convert from a red-green-blue color scheme (that typically used by computers) into a red-yellow-blue “primary colors” color scheme (that typically used by artists)?

I guess I should explain why this is useful.  From the time we’re born our mommies and daddies give us primary colors of red, yellow, and blue.  And we know that yellow and blue make green.  And that the opposite of red is green.  They’re complementary colors.  Anyone who has ever painted, drawn, etc. knows it.  Anyone who has ever taken even a basic art class knows it.  Heck, anyone who’s ever peed into a toiled filled with blue water knows it.  Primary and secondary colors are all around us.  From the day we’re born to the day we die, it’s the color system we know like the backs of our hands.

But those of us who have done graphics work on computers know that colors don’t work that way … on a computer.  Computers don’t use this color system.  Computers use a red, green, and blue color system.  It’s based on light wavelengths.  Red plus green makes yellow.  The opposite color of red is cyan, not green.  (Hence the other related color system based on the opposites of rgb: cyan, magenta, and yellow or cmy.)  It makes sense to anyone who understands light wavelengths, like a scientist.  But to a graphical artist trying to do work on a computer it can be downright confusing if not insanely ludicrous.  I mean just try taking your painting and doing a color inversion.  It doesn’t come out anything like you’d expect.  Your years of artistic knowledge completely fail you when you hop onto a computer.  All because computer monitors are based on light, so computers have always, from the very beginning, been designed in a way to display the proper images on your monitor; not to help you paint a picture.

Now you’d think that every graphics program ever written would therefor have a way to switch from the rgb color system to the ryb color system, because surely graphical artists use graphics programs, right?

Strangely, I’ve yet to see it.  Granted, it’s been a while since I looked at the biggies like Photoshop and Paint Shop Pro (I have used both in the past).  These days I’ve been using GIMP, because it’s free, and once you get used to the eccentricities, it’s darn near as good as Photoshop at nearly everything.

But where I’ve really needed it is in writing GUIs.  Say you’ve got a GUI where you’re describing a crystal.  You want a way to pick the crystal’s color.  You make buttons to choose colors.  Well the names of the colors on the buttons is great, but wouldn’t it be even neater to colorize the buttons themselves?  It’s easy enough to do.  (At least when using Trolltech’s Qt.  MFC makes it a tad more of a challenge.)  So you set the background color of the button widgets to the color defined by the button text.  It’s insanely easy, and pretty effective … for most colors.  But then there’s that nagging problem, some background colors make black text kind of hard to read.  Especially, you know, black.  Black on black is a bit of a challenge to read.  So you want to make the text always a good color.

Now, you can write an algorithm to calculate the luminosity of the rgb value and determine if white or black would contrast more against the button.  Which works.  But it’s kind of a … kluge.  It doesn’t look nearly as professional as it should.

So you take it one step further.  You use an algorithm to get the contrasting color of the rgb value.  It works.  But looks positively awful.  Because scientifically we know the opposing light wavelength to red is cyan, artistically, our whole life tells us that the opposite color of red is green.  And so the rgb-inverse method grates on our sensibilities.  It makes for readable text, but psychologically drives us nuts.

Which is what brings us to the ultimate solution: a rgb to ryb color system change.  Because once we work in an ryb color system, then the inverse of red really is green.  It does wonders for our GUIs, making the text readable and comforting in its simple “rightness”.  And maybe one day it’ll bring relief to graphical artists on computers when you can switch from rgb to ryb in GIMP or Photoshop and suddenly everything makes sense.

So that’s the explanation.  What’s the solution?  I did it once before.  I know I can solve it again.  Here’s what I’ve got so far just scrawling on some paper:

Step 1: Remove the luminosity.   I remember this clearly.  I remember in my first attempt at solving this years back, it took me a while to figure out that you really absolutely need to do this first and foremost, or else your colors will come out in all of the wrong shades.  This part is simple.  Given that red, green, and blue are input variables for your rgb color that you’re converting into an ryb system:


white = min(red, green, blue)
red -= white
green -= white
blue -= white

Now your rgb values contain no white value in them.  You’re working on converting pure color.  So you can take the next step, determining how much raw yellow there is, and how much of the green value is actually representative of the color green:


yellow_part = min(red, green) # In rgb red and green combine in equal parts to make yellow.  So we have to determine how much yellow there is.
green_part = max(0, green - yellow_part) # Now that we know how much yellow there is, we can take the raw yellow away from the green value to determine how much raw green is truly in the green value.

This next step I haven’t tested out yet.  I know that I’m not 100% certain that it’s a final solution because I know that at the very least I haven’t normalized the end values back into the proper scale.  What do I mean?  Well in an ryb system green is essentially yellow plus blue.  But we also have a blue value.  So in a worst-case scenario, we have a maximum blue value of 1/2 green plus blue.  In OpenGL where you’ve got a component scale of 1.0 maximum in rgb, this means blue could have a maximum value of 1.5 in ryb.  So you’ve got to re-scale the values after converting their components.  As I said, I don’t have that worked into the formula yet, but I do have part of the formula:


half_green = green_part / 2 # We know that green needs to be divided into equal parts of blue and yellow, so to optimize the code we calculate half of the remaining green component.
red = red - yellow_part
yellow = yellow_part + half_green
blue = blue + half_green

So at this point you should have the raw component color parts of a red, yellow, and blue color system.  Next up is normalizing the values back to the same scale as was used in the rgb system, and then putting the luminosity back in by re-adding the white value.  Do this and you should be done converting rgb to ryb.  Sometime when I get around to reinstalling Python + PyQt and/or some version of Visual Studio on my computer I may even get around to finishing the conversion code (and testing) in a follow-up Part 2 blog entry.