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.
Recent Comments
Archives
- April 2023
- January 2023
- November 2022
- May 2022
- March 2022
- January 2022
- December 2021
- April 2021
- December 2020
- October 2020
- August 2020
- July 2020
- March 2020
- February 2020
- January 2020
- December 2019
- November 2019
- October 2019
- January 2019
- December 2018
- November 2018
- August 2018
- July 2018
- April 2018
- March 2018
- November 2017
- October 2017
- February 2017
- October 2016
- August 2016
- July 2016
- November 2015
- October 2013
- February 2013
- January 2013
- August 2012
- July 2012
- June 2012
- May 2012
- April 2012
- February 2012
- December 2011