March 10th, 2010

Go to page: 1 2 3 Next

Home-brewed “color spacing”


Note: apparently, not all the code below displays correctly via RSS.

Here’s a home-brewed way to get some of those expensive looking “color aware” effects.

In addition to the original hue/saturation/brightness components, I create six new variables each for hue, saturation, and value (brightness), plus six “weight” values, each corresponding with one of the six colors of a preschooler’s rainbow, ROYGBV (red, orange, yellow, green, blue — fuck “indigo”, but if you wanted me to take it seriously as a color, you should have taught it to me sooner — and violet, even though we called it “purple”):

double h, hueR, hueO, hueY, hueG, hueB, hueV;
double s, satR, satO, satY, satG, satB, satV;
double v, valR, valO, valY, valG, valB, valV;
double wR, wO, wY, wG, wB, wV;

Then at each pixel, you set up the “weight” values depending on what it’s closest to:

wR = wO = wY = wG = wB = wV = 0.0;
if (h >= 0 && h < = 21 ){wO = h /21.0; wR = 1.0 - wO;}
if (h > 21 && h < = 42 ){wY = (h - 21.0 )/21.0; wO = 1.0 - wY;}
if (h > 42 && h < = 85 ){wG = (h - 42.0 )/43.0; wY = 1.0 - wG;}
if (h > 85 && h < = 170){wB = (h - 85.0 )/85.0; wG = 1.0 - wB;}
if (h > 170 && h < = 212){wV = (h - 170.0)/42.0; wB = 1.0 - wV;}
if (h > 212 && h < = 255){wR = (h - 212.0)/43.0; wV = 1.0 - wR;}

Notice that it’s not just selecting one color and going with that; if it’s halfway between red and orange, the red weight would be 0.5, the orange weight would be 0.5, and all the other weights would be zero. After this point in the code, you’re now free to experiment with each value separately. For example, if you want to kill all saturation for the yellow areas, and just turn them grayscale, you would code it this way:

satY = 0;

Similarly, if I want to pump the heck out of the blue areas, I put a line like this:

satB *= 2;

(That’s C++ for “multiply by two”.)

Additionally, I “limit” the red saturation, so that it stays the same up to a point, but doesn’t go any higher; so that skin still looks the same, but bright red ribbons are muted:

if (satR > 96.0) satR = 96;

This example only plays with saturation. I could also mess around with brightness, contrast, etc. in the same way. After this designated “fun sandbox” section is the code that puts together the final values, using the “weight” values:

h = wR*hueR + wO*hueO + wY*hueY + wG*hueG + wB*hueB + wV*hueV;
s = wR*satR + wO*satO + wY*satY + wG*satG + wB*satB + wV*satV;
v = wR*valR + wO*valO + wY*valY + wG*valG + wB*valB + wV*valV;
setpixel(x, y, h, s, v);

I’m not sure exactly how this stacks up against the “color spacing” technology the big companies charge lots of money for, since like I said, it’s based on a Crayola-level color awareness. That considered though, it’s free in both senses of the word… free as in no expensive software to pay for, and free as in freedom to experiment on any of those 18 values with all kinds of math and logic before reducing them back to the original three components.

Much thanks to Darel Rex Finley, since I’m now using his HSP Color Model code to switch the images back and forth between RGB mode and HSP mode. “P” in this case is “perceived brightness”, and more psychovisually accurate than standard “V” or “L” values, but serves the same purpose. Check out his clear and well-illustrated HSP page for an explanation of the difference.

Leonardo da Vinci would have coded in C++.


If I don’t post something now, I’ll never get back into the habit. Test one, test two. Hello!

“Add child back to adjusted parent”


The entire weekend — apart from a couple of alarm-free mornings and one two-hour VIP rehearsal — was spent continuing work on a set of hand-coded image processing algos, to help make reality look more arty-cartoon-surreal for the just-barely-started film project.

What that means is writing stuff like this:

Translating it into stuff like this:

And compiling and running it to make effects like this:

Which you can see animated (in crappy quality) here:

This is not the “complete” effect, just the stage of development that it’s at.

The pseudocode in the top picture was written in a restaurant, and it wound up being one of those rare occasions where the waitress actually asked what I was doing. I did my best to explain it in human terms without dumbing it down to the point of condescension.

Just below that is the code resulting from that handwritten idea, in this case a combined blur/sharpen filter, which happens to be my first-ever recursive function. What that means is, when the function “happens”, it spawns a copy of itself, which also spawns a copy of itself, and so on. As you might guess, this would lead to some kind of crash, if it didn’t have some built-in way of eventually stopping itself — which, I suppose, is part of the thrill of writing it. Honest to golly, though, I went this route because it was the best solution for a problem, not for the sake of flirting with danger.

Most of what you’re seeing are other effects: a median filter, an auto-outlining effect, and a partial desaturation.

Homegrown spectral analyzer


I whipped this spectral analyzer up this weekend. Unfortunately, like everything else coded in Handyland, it doesn’t run in realtime; it has to be rendered as a movie first and then re-synced to the music. I think it’s fun to sit and watch when it’s done, though. Sometimes, if you look hard enough (or sniff enough glue), you can see which peaks correspond to which sounds.

The featured instrumental here is Kid in a Candy Store, from Leave of Absence vol. 1, currently close to being ready for reissue. It was created by slapping a backwards orchestra track onto a drum track, relishing in the serendipity, and then coming up with bass, guitar, and piano bits that would glue it together a little more. This is the music I want playing when the aliens come to pick me up.

Maybe a graphic as simple as this, in combination with lyrics and/or factoids, would lend itself to my earlier idea of using YouTube as an audio player. My only gripe is that I would have to use a workaround if I want the music to be in stereo — at least until YouTube realizes it’s not 1950 anymore.

Sound atoms


So, according to Wunderground, March will in fact be going out like a lamb. It’s just waiting until the last minute to put its lamb suit on. And if I’m waiting until the last minute to have my car inspected (we have to do this once a year in New York state), then it would be hypocritical for me to demand anything else.

I’m working out an idea for a new coding project. It’s not in code language yet. It’s not even in pseudo-code. It’s in English, but not “blogosphere friendly English”, so I’m not going to alienate all y’all by posting said brainstorm here. Essentially it’s a kind of analysis and resynthesis for creatively simulating and manipulating voice-like sounds, which is something I’ve been wanting to do for quite a while now. I’m basing it around something I call “sound atoms”.

Why on earth we as humans feel the need to simulate voices, when we actually have voices, I couldn’t tell ya. But it should be interesting.

Look ma, no bubbles


Now that I’ve taken out the trash…

Flowers

Strangest thing about my old auto-morphing code that I’ve had a couple of demos up for (also on youtube): it’s had an incredibly stupid bug in it all this time, that I would never have noticed if I wasn’t just staring at the code mindlessly. It was transitioning smoothly from picture to picture, but there seemed to be an excessive amount of white or light gray circular areas that would unnecessarily billow up before resolving to the next keyframe. Everything was working correctly except for there was this line of code that I copied and pasted and didn’t finish changing it the way I’d intended to, so instead of getting the data for a, b, c, and d, it was getting “b” twice and leaving “a” at zero.

The reason it didn’t totally cause it to malfunction outright? To interpolate, it takes the data from not two, but four pictures. The keyframe just before the current frame is called “b”, the one just before that is called “a”, the one right after the current frame is called “c”, and the one right after that is called “d”. So you’ve got a, b, c, and d, and the thing you’re filling in is somewhere between b and c.

The reason it uses info going that far back and forward is so that I can make the data curve as it goes along. It’s called a spline curve, when the computer sort of “improvises” a curve to fit an arbitrary set of points, and in order to know how the curve is shaped, you want to know where at least a couple extra points are beyond the ones you’re between. Well, since “a” was treated as though it were all zeros, it would come out of “b” at the right place but at the wrong angle. So the data would always have sort of an “upward lurch” at the beginning of the transitions, thus making all those white and light gray bubbles.

Here is a demo video of a transition sequence that works the way I meant it to.

(Is this the same Keith Handy guy that does the music, or am I at the wrong website?)

Go to page: 1 2 3 Next

Imhotep theme designed by Chris Lin (and then bastardized by the webmaster). Proudly powered by Wordpress.
XHTML | CSS | RSS | Comments RSS