Posts tagged ‘component’

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.

Comcast - A Mixed Bag

So I’ve had the pleasure of being a Comcast customer for a year now. I guess like any company, they have their ups and downs. I’m a techie, so generally I know what I’m doing. Hooking up cables is child’s play to me. The concepts of compressed digital high-def cable signals that have a pause as it has to find a key frame before it can start using the difference frames, I get. So I’m not the kind to complain about flicker or a two second delay before the channel comes in. Nor am I the kind to completely screw up plugging in the component video cables and the audio cables.

So with Comcast, all was good for the first year. Mainly because we (and by we I mean my hun and myself) only had a crappy old CRT tube TV with a crappy little low-def / analog box with composite video only. Simple. Easy. So last century.

Then we ‘upgraded’. We picked up a high-def 40″ TV a while back, and so when our year of Comcast contract ended and they wanted to bump up our prices (annoying, but normal) we switched packages to include high-def. Which required a new box.

Well the trip to pick up the new box was a pure joy. And by joy of course I mean sarcastic for hell. We had to wait in line for at least a half hour with every hick that could be drug up from the worst of society, a real bunch to make Jerry Springer proud. No, I am not exaggerating. It really was that bad. But oh, wait, it gets better. We finally get up to the counter to talk with a customer service representative - who was a nice and knowledgeable lady by the way - when the pest exterminator sprays for poison. Because one of those winning customers brought in a box infested with roaches. Lovely.

(A strange fact that seems to be cropping up repeatedly lately for me, is that bugs - especially roaches - are attracted to electro-magnetic fields. Which kind of makes me wonder how those pest control plug-in thingies are supposed to actually scare them off if bugs are attracted to emf.)

So finally we get the new high def box. Yipee! All we have to do is phone up their tech support to ping the box with the new activation code and tada - high def! So we’re told.

Well I hook up the box with glee, happy to have component video instead of HDMI because my YSP-1000 surround-sound speakers have a component video pass-through, but not an HDMI. (The reason the speakers have a video pass-through is two reasons, one is that the audio signal can be ever so slightly delayed because of audio decoding, so the pass-through re-syncs the audio and video, and the other reason is how else are you going to see the speaker’s menus if you don’t have a video connection to the TV.) So I wanted component video. The box is hooked up. And working … ish. It works for all of our old channels, but we get no high-def channels. Well, okay, so we haven’t activated it. There’s a code that triggers in the box what you get and don’t get. Time to phone in and fix us up!

I phone. Stupid automated directory, none of options there even sound remotely related to “activate my box” or “tech support”. But hey, if I choose the wrong option they’ll just forward me in the right direction, so a wild guess later and we have a real live person. Well, no. Actually we go on hold. I kid you not, twenty minutes on hold. Of REALLY loud elevator music of a decibel an order or two of magnitude higher than the phone directory’s volume so that it nearly deafens me as it blasts me with the most boring music man can create. The irony of it is the only thing that keeps me from getting well and truly pissed.

But lo, we do finally get a human being. She’s relatively nice. It’s a simple procedure. And just as the box is supposedly being activated - BAM - dial tone. We got hung up on? We wait for the Comcast tech to call us back while we try to dig up the number to call again. No callback. And whatever happened, it didn’t change a thing. The box still doesn’t get our high-def channels.

So we go through the tedium of calling up again. Only a 15 minute wait this time. This woman at least has more of a clue, as she tells us that the programming signal they send out resets all devices, including the VoIP phone. So when we get disconnected again - because it’s not a matter of if since we’re calling from our Comcast phone - she’ll call us back. (Might I add … BRILLIANT design Comcast.)

It turns out the reason we don’t have high-def channels is that we aren’t signed up for high-def. Umm … what? We called, made the switch, and picked up the new high-def box because we were told that we’d need it to get our new high-def channels in high-def. Which I know is true because you don’t get high-def video out of a single little lonely composite cable - which is all our crappy little box had. Why would we be told to get a high-def box if we hadn’t signed up for high-def? And how would we know the exact price change for the upgrade? Hmm … clearly someone at Comcast forgot to enter the customer information into their database. Brilliant. But, luckily, this phone customer service rep, being apparently smarter than the other two we’ve dealt with so far (I’m not counting the one we talked to in person in this) is actually capable of doing it right this time. So we get switched to high-def. And she re-pings the box with the new activation to include high-def channels, all the while complaining about how complicated the new code entry is.

We get cut off. It’s inevitable. Change the hardware code, lose your Comcast phone connection for a few seconds while the device reboots.

But at least she calls back. That’s a nice improvement.

Only now the box isn’t working. We have no channels. None. The TV guide information is there. But each channel just says something like “please wait - this channel will be available shortly” like it’s just having decoding problems or something. Every single channel.

I try to convey this problem to her.

She spends the next half hour trying to convince me that the connection between Comcast and the box is working fine, the box is programmed correctly, and so the problem clearly is that I have the box hooked up to the TV incorrectly. Uh-huh. Which is why I get a video signal from the box. Which is why just before she re-programmed the box it was working perfectly on all of the analog channels, receiving audio and video. Right. Clearly I screwed it up. (That’s sarcasm by the way.) We go around the issue over and over. I’m adamant that I have it hooked up right. She’s adamant that she did her job right and it’s my fault. And whenever I get aggravated and my tone changes, she quips things like, “I’m only trying to help you, sir.” Because she’s been such a great help so far. Telling me how to connect component cables and asking me if I have the coax connected to the back of the box clearly is helping me diagnose the problem. (Again, sarcasm.) I even get her to admit that she has no diagnostic skill, she’s simply going through a written list. And that’s helpful, how?

Well, we both get tired of the game. She schedules a tech to make a house call. Thank god!

So a couple of days later the Comcast guy is set to show up. He calls up to ask if it’s okay if he comes early. Cool! Hell yeah! He shows up. He’s baffled. I’ve got it set up right. It’s most definitely working between box and TV. The problem is clearly between box and Comcast.

He fiddles. He has them re-ping, service-ping, yada yada. Nada. So he swaps boxes. One ping to the new box later, all is perfect. Cool. Friendly and helpful. I like.

So whatever the problem was, it was either that Ms. Genius sent codes that FUBARed the box, or the box’s flash memory was literally on the edge of one too many writes. Either way, it wasn’t my fault, like she’d been trying so hard to convince me. And the house-call tech, who actually knows what he’s doing, was able to resolve the problem unlike the phone support who just blamed me for the problem instead of just admitting culpability and/or she simply doesn’t know.

Moral of the story?

Dealing with Comcast over the phone sucks. Period. They don’t have a clue what they’re doing.

Dealing with Comcast in person gets results. The people that deal with live customers are actually trained, intelligent, and personable.

So if you have Comcast, and you have a real problem, I suggest taking it to a real person. Chances are the customer service person on the phone will just frustrate you more than help you.

That’s my experience anyway.

Oh, as I was talking with the house-call tech, one tip I learned is that if you’re upgrading to a high-def box or DVR and you absolutely need HDMI, tell them that you absolutely need HDMI. Because if you don’t specify that you need it they’ll just hand you whatever high-def box is available, be it brand new with HDMI or years old recycle without. They don’t care. And if you don’t tell them that you care, how are they going to know to choose one more specific than that it’s high-def?