Skip to main content

WOLFRAM / Making Text With Rainbow Color



Continuing with my Wolfram Mathematica Trial Experience...

I watched and went through some more Mathematica introduction videos, read lots of Mathematica documentation and also going through the Wolfram Lab Online again a few times.

There are some major learning curves and Mathematica is a lot different from normal programming language. Sometimes there is a lot of interesting "shortcuts", say like: FindFaces[], WordCloud[].

Sometimes I got a little confused on how one can do iterations. Normally FOR LOOP concept is introduced early, but in Wolfram, because everything is EXPRESSIONS and ENTITY (like OBJECTS), sometimes it gets quite quirky.

Mind you I am still in the first impression and having to look at many tutorials. Lots of NEAT EXAMPLES from documentation, but sometimes I got lost.

I found Wolfram to be really awesome with LIST and generating list. It's almost too easy when it works visually. I cannot explain them with my own words yet, but there are 3 expression to keep being aware of when working with LIST:

  • Table[]
  • Map[]
  • Apply[]


Table[] can quickly generate pair numbers, row and column tables of sequences.

For my Rainbow Text practice, I thought that Table has potential to quickly create the result I wanted, however, I am stuck with how to break and iterate over STRING.

(You see above how "colors" can just be stated like that? The power of Entity in Wolfram.)

There is this Characters[] expression that can break Text String into List of Characters. However, unlike in Python, I cannot think of FOR LOOP setup in Wolfram. Maybe it is different?

for every character in text blah:
   character.color = ???
   character.size = ???

I tried all kinds of combination and probably missing something here.



Although Style[] itself can easily output Text with custom Size, Color, and Character/Text like above. However, how to break into character?

RAINBOW TEXT

Really I think it should not be too hard, but it requires some Wolfram kind of thinking. There must a very elegant syntax expression way to get what I want, but for now I will go the hard way.

I realized I can do this below (the Row[] is just so that we can see the Text Output without bracket and commas):

Cool, now knowing the above, we can probably prepare the data generator to pair those 3 required values {text character, text size, and color}.

Below, I even found out about Transpose[] expression by accident. This could be very useful in many cases. Although I wonder if this is expensive to calculate and generate in Wolfram? Because at some point I can see the RAM usage goes up the roof more than 13GB? Or maybe it was a bug.


Anyway, the idea is to generate the values needed to be applied to multiple list.

  • Prepare a string to be made into Rainbow Colored Text output
  • Break the TEXT STRING into CHARACTERS, be aware about the total character numbers
  • Now, I generate the numbers (it's going up from 0 to 1 in HUE, easy actually)
  • Then I generate random size numbers list.
  • Combine everything and using Style[] we got the job done.

I guess this is interesting exercise.

I don't know, maybe it is discussed or mentioned in the documentation somewhere, but digging Mathematica and Wolfram Language is interesting.

I wish I can ask mentor, but well, part of self learning is to keep searching for answer. Maybe ask at Mathematica Stack Overflow or check at Mathematica or Wolfram forum out there?

SMALL TRICK I LEARN...

Lots of cool things can be done just by using a single liner expression in Wolfram. I believe Mathematica is one language that introduces this "Notebook" workflow that combines users own regular text with code together. Don't know it's probably iPython first. 

I found out that we can right click and copy a cell line (that weird line on the right side. Instead of having to highlight.

Beginner programmers that are using Python and Jupyter Notebook may be shocked a bit when they normally work in single liner and then having to switch to multi lines and using Variables. Warn you.

What's cool is once we have a working program, we could combine all the expressions, variables, and functions all into one. Using semicolons (;) actually allows user to keep variable without having to output the result until the last line.



So far though, I believe I have been doing Procedural Programming. Still basic, however it gives an interesting output. 

And one thing I really like with Wolfram is how instant and visual the data feels like.

SOMETHING CRAZY

In Wolfram Language, sometimes making everything as numbers and trying to visualize the output in all kind of ways, is almost pushed. Being crazy and try things are much recommended.

Below, I tried visualizing the Character Code (sometimes called ASCII) of my text, and turn it into Wolfram sound output as well.


The range that SoundNote[] can take is limited, so that's something to keep in mind. But something like below seems to work okey.

mytext = "Awesome"
EmitSound[
 Sound[SoundNote /@ Flatten[RealDigits[ToCharacterCode[mytext], 50]], 
  4]]
Print[Flatten[RealDigits[ToCharacterCode[mytext], 50]]]

Comments

Popular posts from this blog

PYTHON / OpenCV, Recreate Uncanny Manga - Anime Style

Can you tell what it is? Computer Vision. Yesterday, I spend almost whole day exploring this opencv module using Python. What I discovered was revealing. Even at the very basic level, I could produce some interesting Image and Video manipulation using all the code collected from documentation and many, many blog tutorials. If you are a total noob like me, I am still getting used to knowing that the CV in OpenCV means Computer Vision! Actuallly, I recalled that I did try to get into OpenCV few years back ago, when I knew no Python and when Python opencv module was probably still early. It was all C++ code and it was a little bit too hard for me. I read a couple of books about opencv at the library, I did not understand a single thing. That was back then. Today, for strange reason, with a bit of knowledge of Python, I can go a little further. EDGE DETECT IN OPENCV Me holding you know what. What leads me this far is my curiosity on how we can replicate Wolfram Langu

PYTHON PROCESSING / It only really begins ...

Back into Processing again, with Python! While Daniel Shiffman is continuously inspiring us with his CODING TRAIN series on YouTube, using Processing with Java and Javascript language, I decided to free my mind and trying to really do something using Processing and Python language. Installing Python language version on Processing 3 is easy enough, just first download the latest Processing and install the Python language mode via Add Mode button. Other link that might help: https://github.com/jdf/processing.py http://py.processing.org/tutorials/ BLANK MODE As soon as Processing Python Mode, opens up and running I am presented with a  blank environment. Suddenly I recalled my journey in learning programming from zero until now... With Python, outside Processing, commonly people will be introduced to Python IDE or IDLE environment. Something that looks like Console Window or Command Prompt, where we type single liners command. Python Command Line and IDE normally have t