# How to make text flash for a short period

How could I display text that flashed red for a half second or so and then reverted to black? (Or was put in bold and reverted to normal, etc.)

-
You already posted 6 questions, voted only four times, and never accepted an answer as correct. Is there anything wrong with your experience in the site? – belisarius has settled Jun 28 '12 at 15:08
Seraph, please note that the community not just expects you to ask questions but also to weigh and occasionally accept the input that you get from others. You can even accept answers for your older questions by clicking on the checkmark next to the answer you prefer. – István Zachar Jun 28 '12 at 15:47
More importantly, it'd be nice to cast your vote on other people's questions, and their answers as well, if you read those posts. meta.mathematica.stackexchange.com/questions/22/… – Szabolcs Jun 28 '12 at 19:36
I have gone back through my questions and marked my favorite answer for some of them. I do not check Math SE very often (a situation that is not likely to change), and when I have found a question I could answer, in each case someone has already answered it. – SixWingedSeraph Jun 29 '12 at 21:29

 PrintTemporary[Style["text", Red]]; Pause[2]; "text"


EDIT: This looks too plain in comparison to all the cool effects that can be achieved with methods used in other answers. The following is an attempt to arm-twist PrintTemporary to perform similar tricks:

 Scan[(temp = PrintTemporary[#]; Pause[.1]; NotebookDelete[temp]) &,
Style[Rotate["text", #[[1]]], Bold, 60, FontColor -> #[[2]],
FontFamily -> "SketchFlow Print"] & /@
NestList[{Plus[#[[1]], 20 Degree], Darker[#[[2]]]} &, {0 Degree,Red}, 18]]; "text"


(Note: try it in the last cell of your notebook to avoid flickering cell sizes).

-

By specifying different values for time and frequencyInverse, the behavior of flashing can be finetuned.

time = 100;
frequencyInverse = 4;

i = 0;
Dynamic@Style["TESTESTEST", Bold, RGBColor[color, 0, 0]]
RunScheduledTask[(i = i + 1; color = Rescale[Sin[i/frequencyInverse], {-1, 1}];
color = 0]), {.01, \[Infinity]}];


(Not that the actual flashing is faster in Mathematica, the slowdown is because of exporting it to GIF.)

With Clock:

time = 1; (* period length *)
iterations = 5; (* number of successive flashes *)
img1 = DensityPlot[Sin[x] Sin[y], {x, -4, 4}, {y, -3, 3},
ImageSize -> 50, Frame -> False];
img2 = DensityPlot[Sin[x] Sin[y], {x, -4, 4}, {y, -3, 3},
ColorFunction -> "SunsetColors", ImageSize -> 50, Frame -> False];
Dynamic@Overlay[{img1,
SetAlphaChannel[
img2, (1 - Abs@Clock[{-1, 1, .1}, time, iterations])]}]


-
I know you can Import[] the DisplayDurations from an animated GIF, but don't remember if you can Export[] them – belisarius has settled Jun 28 '12 at 15:55
@Robert: Thanks for pointing that out! – István Zachar Jun 28 '12 at 16:29
@belisarius: I've cheated, so the gif no looks more like the animation in Mathematica :) – István Zachar Jun 28 '12 at 16:30
For version 7 users: Dynamic @ Show[img1, Graphics[{Opacity[(1 - Abs @ Clock[{-1, 1, .1}, time, iterations])], img2[[1]]}]] – Mr.Wizard Jun 29 '12 at 9:52

Use Dynamic and Refresh:

Dynamic[Refresh[
Style["text",
FontColor -> If[Mod[Round[AbsoluteTime[]], 2] == 0, Red, Black]],
UpdateInterval -> 1]]

-
Style["TESTESTEST", FontColor -> Dynamic[If[Clock[] 2 - 1. > 0, Red, Black]]]


If you want it to flash only once

Style["TESTESTEST", FontColor -> Dynamic[If[Clock[{-1, 1, 1}, 1, 1] > 0, Black, Red]]]

-
I prefer your codes since it is so simple. – yulinlinyu Jun 29 '12 at 1:17

As a variation of answer to this question, here is an approach I used for lectures. Here are the functions Ac and Pl that either accentuate or leave the text plain:

Ac[expr_] :=
DynamicModule[{c1 = 0}, EventHandler[
Dynamic[
If[c1 == 0,
Style[expr, Black, Plain, 22, Italic] // ExpressionCell,
Dynamic @ If[Clock[1, 0.7, 2] < .5,
Style[expr, Gray, Plain, 22, Italic] // ExpressionCell,
Style[expr, RGBColor[0.8, 0, 0], Bold, 22, Italic]] // ExpressionCell]],
{"MouseDown" :> (c1 = c1 /. {0 -> 1, 1 -> 0})}
]];


and

Pl[expr_] := ExpressionCell[Style[expr, Black, Plain, 22, Italic]];


And here is an example of its application:

F = Row[{"AA" // Pl, "+BB" // Ac, "+CC" // Pl, "+DD" // Ac}]


The strings "+BB" and "+DD" are accentuated. The accentuation is shown upon the mouseclick on the corresponding elements.

-

It's worth pointing out that several of the other answers keep dynamically re-evaluating an expression until infinity.

Here's a solution that fades out the text gradually, and more importantly: it does not keep re-evaluating the Dynamic expression until infinity. The third argument of Clock (and the fact that Dynamic is smart when handling Clock) can be used to terminate the evaluation.

flash[expr_] :=
Dynamic[Style[expr, Blend[{Red, Black}, Clock[{0, 1}, 1, 1]]]]

flash["Boo!"]


(Update: I overlooked that István's original answer uses Clock in the same way.)

-
My second example uses a finite number of iterations as the third argument of Clock. But it is an interesting question: how to turn a dynamic expression to static after a limited number of updates... – István Zachar Jun 28 '12 at 21:04
@István You're right, I was not attentive enough. Will edit. – Szabolcs Jun 28 '12 at 21:07