9

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.)

István Zachar
  • 47,032
  • 20
  • 143
  • 291
SixWingedSeraph
  • 627
  • 3
  • 10
  • 14
    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? – Dr. belisarius Jun 28 '12 at 15:08
  • 2
    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. http://meta.mathematica.stackexchange.com/questions/22/vote-early-vote-often – Szabolcs Jun 28 '12 at 19:36
  • 3
    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

6 Answers6

18

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}]; 
  If[i == time, RemoveScheduledTask[ScheduledTasks[]]; 
   color = 0]), {.01, \[Infinity]}];

enter image description here

(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])]}]

enter image description here

Robert Miller
  • 360
  • 1
  • 4
  • 9
István Zachar
  • 47,032
  • 20
  • 143
  • 291
14

Use Dynamic and Refresh:

Dynamic[Refresh[
  Style["text", 
   FontColor -> If[Mod[Round[AbsoluteTime[]], 2] == 0, Red, Black]], 
  UpdateInterval -> 1]]
ragfield
  • 1,365
  • 7
  • 11
9
 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).

kglr
  • 394,356
  • 18
  • 477
  • 896
9
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]]]
Rojo
  • 42,601
  • 7
  • 96
  • 188
8

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.

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
Alexei Boulbitch
  • 39,397
  • 2
  • 47
  • 96
6

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.)

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
  • 1
    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