Closed Thread Icon

Preserved Topic: Need some help optimizing a function (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=18407" title="Pages that link to Preserved Topic: Need some help optimizing a function (Page 1 of 1)" rel="nofollow" >Preserved Topic: Need some help optimizing a function <span class="small">(Page 1 of 1)</span>\

 
DocOzone
Maniac (V) Lord Mad Scientist
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

posted posted 05-29-2002 13:30

OK; InI came back at me with some killer tweaks on my "doc5k" page, and I implemented some of his enhancements, most notably abandoning the hex (ie: #ff9, etc.) notation, and instead using the rgb(255,255,144) style notation, which allowed me to eliminate some codes, and added some new, making for a break-even situation in terms of bandwidth, but allowing me to cut some processing time and make smoother transitions of my colors.

Now I have this one code situation that I run into a lot of time, I need to set some variables based on a random number, and I end up with this ugly, brute force method, like this...

function rC(j,v){
v*=7;j*=32;
if(c==0)cL=j+","+v+","+153;
if(c==1)cL=153+","+v+","+j;
if(c==2)cL=v+","+102+","+j;
if(c==3)cL=51+","+v+","+j;
if(c==4)cL=153+","+j+","+v;
if(c==5)cL=v+","+j+","+99;
if(c==6)cL=j+","+v+","+33;
if(c==7)cL=j+","+0+","+v;
if(c==8)cL=255+","+j+","+v;
return cL;}

Yuck! This is so ugly, I mean, yuck! There has to be a better way, there just has to be. Does anyone know what it might be? FWIW, I may just abandon this whole color-choosing option, and instead borrow another of InI's tweaks, only instead of brightness have the colors constantly morph along, that'd be cool. Still, this coding situation comes up frequently with me, I'm a control freak so I want to set fixed values, but... yuck! Help an old fart of a programmer out, please!

Your pal, -doc-

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 05-29-2002 14:35

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 05-29-2002 14:41

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

DocOzone
Maniac (V) Lord Mad Scientist
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

posted posted 05-29-2002 15:26

Really? I missed that part, of I was thinking of filling in my remaining space with some "lorum ipsum dolor" space, just to show off, heh. Knowing this, I'll robably go through it again towards the end and strip even more out, eliminating any of those 3 or 4 later varnames frst off! :-) I have this picture of the colors constantly flowing and changing, one to the other, that could be cool, methinks, and should be fun to code.

Your pal, -doc-

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 05-29-2002 15:55

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 05-29-2002 19:30

fark I dunno if this is gonna help much. but I'm thinking some kind of map.
(Yeah I don't like eval's either but that's what I got)

function rC(j,v){
v*=7;j*=32;
vals = array('j+","+v+","+153', '153+","+v+","+j;' ... etc.
return eval(vals[c]);
}

I think in combo maybe with Slime/Ini's idea for the common v+j stuff wrapped in one var. Even without it this might work.



.:[ The Tao of Steve ]:.
Be Desireless
Be Excellent
Be Gone
...................................

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 05-29-2002 20:14

burp



.:[ The Tao of Steve ]:.
Be Desireless
Be Excellent
Be Gone
...................................

[This message has been edited by bitdamaged (edited 05-29-2002).]

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 05-29-2002 22:15

When I first saw that in your code, I started changing it to a switch block. I soon realized that although that would help readability, it would end up making the code longer.

You might try this:

function rC(j,v){
v*=7;j*=32;;
if(c==0)cL=[j,v,153];
if(c==1)cL=[153,v,j];
if(c==2)cL=[v,102, j];
if(c==3)cL=[51,v,j];
if(c==4)cL=[153,j,v];
if(c==5)cL=[v,j,99];
if(c==6)cL=[j,v,33];
if(c==7)cL=[j,0,v];
if(c==8)cL=[255,j,v];
return cL.join(',');}

But, I dunno, I'm not sure there's a better way... you could first store all the arrays I have there in a big container array, and then just return cL[c].join(','), that might be better... let's try that:

function rC(j,v){
v*=7;j*=32;;
cL=[[j,v,153],[153,v,j],[v,102, j],[51,v,j],[153,j,v],[v,j,99],[j,v,33],[j,0,v],[255,j,v]];
return cL[c].join(',');}

I might have the syntax wrong for array litterals; it's possible it's supposed to be {} instead of [].

Hugh
Paranoid (IV) Inmate

From: Dublin, Ireland
Insane since: Jul 2000

posted posted 05-30-2002 13:04

Just a thought , maybe you should make it some sort of colour picker to give it funcionality , which is one of the categories it will be marked in isnt it. Hue,Saturation and Lightness slider bars would be cool, to save space, the sliders could be flash, I heard you could send variables from flash to JS etc.. am I wrong ?
Also , if the colours faded between the settings it would be pretty damn cool.

P.s I noticed the wee icons above each post have changed, I think it looks better.

« BackwardsOnwards »

Show Forum Drop Down Menu