Post Image
By Matt GaidicaNovember 24, 2018In Uncategorized

Infinite Highlight Color Marks in Ruby (modulo based)


Consider the case where you have many sentences pieced together from different sources and want to highlight each one of those sentences based on the source number. In HTML, the standard for highlighting text utilizes the <mark> tag with the background-color attribute set to a light yellow color.

My background color is set using style="background-color: hsl(50,90%,90%);"

The first value in hsl() is a value in degrees for hue which ranges from 0 to 360, spanning from red to green to blue and back to red in circular form. The goal of the following function (i.e., line of code) is to return maximally different hues based on the total amount of colors you want to use.

If the first color is a standard yellow (used above) then startHue is 50. nColors is the total number of colors you want to use and ii is the current color you want to select. Consider where nColors = 6:

This is from the first source (where ii = 1).1 The second source has a different color.2 So does the third!3 All of the colors are meant to contrast.4 Unless nColors is very large.5 This is the where ii = 6; that’s all of them.6

Pro Tip: If you want to selectively highlight text based on a word or regex pattern in Ruby on Rails there is a great Text Helper called highlight.

svgMATLAB Line Colors for The University of Michigan
svgEqualizing Sampling Rate for 1-D Correlations in MATLAB