Re: Factor

Factor: the language, the theory, and the practice.

Rainbows

Friday, June 2, 2023

#colors

Rainbows are awesome, especially the ones that are double rainbows which can be quite intense when they are a double rainbow all the way across the sky. They can also be awesome when they show up as rainbow flags which are used to indicate that a place is welcome, accepting, and safe for people.

Given that this is Pride Month, it might be fun to make some rainbows today using Factor.

I bumped into this nice tutorial on making annoying rainbows in javascript that has some background on color theory including links to how color vision actually works as well as detailed science on light and the eye and a “better rainbow method” called the sinebow.

After implementing a lot of color support for Factor, I get nerd sniped sometimes when it comes to colors. Instead of using HSL colors, lets just use the more common RGB color model to make some rainbows!

:: rainbow-phase. ( str phase -- )
    str >graphemes :> chars
    2pi chars length / :> frequency

    chars [| s i |
        frequency i * 2 + phase + sin 0.5 +
        frequency i * 0 + phase + sin 0.5 +
        frequency i * 4 + phase + sin 0.5 +
        1.0 <rgba> :> color

        s "" like H{ { foreground color } } format
    ] each-index nl ;

: rainbow. ( str -- ) 0 rainbow-phase. ;

This supports Unicode by calling >graphemes to split a word by grouping on visual characters.

And, it looks like this:

Happy Pride Month!