SRL Archive - SRL Snippets

fabText(); Never use boring monochrome text again!


Citrus

2015-04-07 11:16:06

EDIT(5/9/2015): condensed everything into a single procedure. added shiftPerc variable.

I was bored yesterday so I started messing around the the colors on my progress reports. This is the result…

fabText

What does it do? It makes your text fabulous of course!

like this:

It's a modified version of drawText() with a few additional parameters to input.

Here is what you need to give it:
(txt: string; pnt: TPoint; font: string; color: TColor; shiftPerc: extended; leftToRight, shadow, random: boolean);
where txt, pnt, font, color, and shadow are the same as in drawText(), and leftToRight and random are new.
leftToRight just determines the direction of the hue shift (true=ROYGBIV, false=VIBGYOR).
shiftPerc determines how much the hue is shifted. 50 –> full rainbow. I almost always use 5.
random sets the starting hue to a random value.
The picture above has all 3 booleans set to true:
smartImage.fabText('bars: ' + intToStr(loadCount*28), point(mainscreen.x1+1, mainscreen.y1+15), loginchars, col, true, true, true);

If you can't handle the full rainbow, don't worry! The hue can be shifted by any amount with shiftPerc
Here is an example that shifts random colors between their corresponding analogous colors (30 deg hue shift):

with random set to false:

To get started, head on over here and pick a starting color.
Note: the hex values need to be reversed for some reason. I wanted to use 99ffff, but to get that color I had to use TColor($ffff99). Maybe someone else can explain this.
I recommend picking something with high saturation and lightness. All of the pictures above are at 100% saturation and 80% lightness.

Here is the code:
procedure TMufasaBitmap.fabText(txt: string; pnt: TPoint; font: string; color: TColor; shiftPerc: extended; leftToRight, shadow, random: boolean);
var
i, j, a, b, len: integer;
step, deg, h, s, l: extended;
aH: TExtendedArray;
tpa, tpaShadow, temp: TPointArray;
atpa: T2DPointArray;
rainbow: TColorArray;
hit: boolean;
begin
tpa := tpaFromText(txt, font);
offsetTPA(tpa, pnt);
tpaShadow := copyTPA(tpa);
offsetTPA(tpaShadow, point(1, 1));
self.drawTPA(tpaShadow, __SHADOW_COLOR);

a := tpa.getBounds.x1;
b := tpa.getBounds.x2+1;
tpa.sortByXValue(true);
setLength(atpa, (b-a));
for j := a to b do
begin
hit := false;
for i := 0 to high(tpa) do
begin
if j = tpa[i].x then
begin
atpa[j-a].append(tpa[i]);
hit := true;
end
else if hit then break;
end;
end;

colorToHSL(color, h, s, l);
if random then h := random(100);
len := length(atpa);
if len < 1 then exit();
deg := shiftPerc;
if len = 2 then aH := [h-deg, h+deg];
if len = 3 then aH := [h-deg, h, h+deg];
if len < 4 then exit();
step := (deg/((len-((len mod 2)+2))/2));
for i := 0 to (len-1) do
begin
if leftToRight then
aH.append((h-deg)+(i*step))
else
aH.append((h+deg)-(i*step));
end;

setLength(rainbow, (high(aH)+1));
for i := 0 to high(aH) do
rainbow[i] := hslToColor(aH[i], s, l);

for i := 0 to high(atpa) do
self.drawTPA(atpa[i], rainbow[i]);
end;

Feel free to use and modify this as you wish.

Citrus

2015-04-07 11:32:11

@KeepBotting
@bonsai
and maybe @Taric?

Ian

2015-04-07 11:40:23

Looks nice :)

KeepBotting

2015-04-07 11:52:30

Awesome, rep+

Turpinator

2015-04-07 12:01:42

function hueHueHUE()

sat sat sat.
got me. nice.

Renzanity

2015-04-07 12:05:48

Def a rep+

Citrus

2015-04-07 12:08:29

From: Turpinator
function hueHueHUE()
got me. nice.



Citrus

2015-04-07 12:34:30

just updated code and attachment

Flight

2015-04-07 13:11:59

Haha that's a unique idea and you put it together nicely. Good job pal.

Citrus

2015-04-07 15:21:01

So, uhh, quick question. I'm calling fabText() 5 times in succession to display my progress report, but
  if randomCol then h := random(100);
is giving me the same "random" number all 5 times. Why? I'm guessing it's too fast or something? If I add a small wait it fixes it but I feel like I shouldn't have to do that.

Taric

2015-04-07 15:47:00

From: evilcitrus
So, uhh, quick question. I'm calling fabText() 5 times in succession to display my progress report, but
  if randomCol then h := random(100);
is giving me the same "random" number all 5 times. Why? I'm guessing it's too fast or something? If I add a small wait it fixes it but I feel like I shouldn't have to do that.


randomRange(0,100);
Should give you a random number each time…

Totally putting this in my smelting script when I get back from the Recruiters Office today. #Sexy #Iwantyourbabiesinsideofme

Citrus

2015-04-07 15:58:22

Totally putting this in my smelting script when I get back from the Recruiters Office today.


I had a feeling you would like it.

From: Taric
randomRange(0,100);
Should give you a random number each time…


It's pretty inconsistent. Sometimes all 5 numbers are different, sometimes they're all the same, sometimes they're doubled up. I added a 10-15 ms wait to the procedure and it seems to be fixed.

KeepBotting

2015-04-07 16:17:22

From: evilcitrus
I had a feeling you would like it.



It's pretty inconsistent. Sometimes all 5 numbers are different, sometimes they're all the same, sometimes they're doubled up. I added a 10-15 ms wait to the procedure and it seems to be fixed.


Shot in the dark here but you could try calling
randomize();
before you generate your random number

Citrus

2015-04-07 16:21:07

From: KeepBotting
Shot in the dark here but you could try calling
randomize();
before you generate your random number


Yeah I tried that but it didn't seem to help.

KeepBotting

2015-04-07 16:34:07

From: evilcitrus
Yeah I tried that but it didn't seem to help.


*shrug*

program new;
var
i, j:integer;

begin
for i := 0 to 49 do
begin
num := random(10);
writeLn(j);
end;
end.

That generates 50 numbers, different one each time & no wait in between, try using it like that

bonsai

2015-04-07 17:09:10

Awesome release! I have a soft spot for silly code that does entertaining things.

I almost feel like Taric is the only one who should be able to use something this fabulous.

Can you explain stupidsort? Seems like you're sorting the X value after sorting the X value?

From: evilcitrus
… giving me the same "random" number all 5 times. Why? I'm guessing it's too fast or something? If I add a small wait it fixes it but I feel like I shouldn't have to do that.


From: KeepBotting
*shrug*
program broken;
That generates 50 numbers, different one each time & no wait in between, try using it like that


I suspect that was a phone post. The writeln would cause a delay too.

This one runs fine, though. evilcitrus: if you writeln your 'h' value you will see it changing.

What is probably happening is you are painting all 5 of them so fast to smartimage that it doesn't even update any of them except the last one. The delay you're needing is the time in between smartimage paints.

program new;
var
i :integer;
tia: TIntegerArray;

begin
setLength(tia, 50);
for i := 0 to 49 do
begin
tia[i] := random(10);
end;
writeln(tia);
end.


Compiled successfully in 562 ms.
[4, 7, 6, 1, 8, 9, 9, 0, 0, 9, 4, 5, 8, 7, 3, 3, 8, 2, 2, 1, 3, 7, 0, 7, 5, 8, 0, 0, 6, 4, 6, 9, 2, 3, 3, 7, 4, 1, 7, 0, 4, 1, 7, 4, 5, 8, 3, 2, 6, 0]

Citrus

2015-04-07 17:26:47

From: bonsai
Can you explain stupidsort? Seems like you're sorting the X value after sorting the X value?


It groups the TPoints from the tpa into columns (same X value) and makes each column a separate tpa in an atpa. Maybe there's a better way to do that, but I was just making it up as I went along.

if you writeln your 'h' value you will see it changing


I did that and was getting things like
37
37
61
61
5

Taric

2015-04-07 21:54:45

10/10 Would take to my mothers wedding.


Threshold

2015-04-10 11:42:11

Definitely going to try and use this next month when I work on scripting lol. Probably makes it 3x more fun.

Citrus

2015-05-09 16:49:43

Slight update for anyone who cares.