Skip to content

Instantly share code, notes, and snippets.

@zauguin
Last active March 9, 2023 19:15
Show Gist options
  • Save zauguin/392687c327c5345458520a9f9ae60287 to your computer and use it in GitHub Desktop.
Save zauguin/392687c327c5345458520a9f9ae60287 to your computer and use it in GitHub Desktop.
% Siavoosh Payandeh Azad Jan. 2019
% 2023-03-09: Adapted for use with luaicc by Marcel Krüger
\ProvidesPackage{highlight}[Cell background highlighting based on user data]
\RequirePackage{etoolbox}
\RequirePackage{pgf} % for calculating the values for gradient
\RequirePackage{xcolor} % enables the use of cellcolor make sure you have [table] option in the document class
% Color set related!
\definecolor{high}{HTML}{ef3b2c} % the color for the highest number in your data set
\definecolor{low}{HTML}{fff7bc} % the color for the lowest number in your data set
\newcommand*{\opacity}{90}% here you can change the opacity of the background color!
%======================================
% Data set related!
\newcommand*{\minval}{0.0}% define the minimum value on your data set
\newcommand*{\maxval}{1.0}% define the maximum value in your data set!
%======================================
\ExplSyntaxOn
\lua_load_module:n{parse_icc_tex}
\LoadProfile \sRGBprofile {sRGB.icc}
\LoadProfile \lightnessProfile {lightness.icc}
\cs_new_protected:Npn \xcolorAsSrgb #1#2 {%
\extractcolorspec{#2} \l_tmpa_tl
\exp_last_unbraced:No \convertcolorspec {\l_tmpa_tl} {rgb} \l_tmpa_clist
\tl_set:Nx #1 {
\sRGBprofile
\clist_use:Nn \l_tmpa_clist {~}
}
}
\ExplSyntaxOff
% gradient function!
\newcommand{\gradient}[1]{
\gradientcell{#1}{\minval}{\maxval}{low}{high}{\opacity}%
}
%======================================
% gradient function single cell!
\newcommand{\gradientcell}[6]{%
% The values are calculated linearly between \minval and \maxval
\ifdimcomp{#1pt}{>}{#3pt}{#1}{%
\ifdimcomp{#1pt}{<}{#2pt}{#1}{%
\pgfmathparse{int(round(1000*(#1/(#3-#2))-(\minval*(1000/(#3-#2)))))}%
\xcolorAsSrgb \lowRgbColor {#5!#6}%
\xcolorAsSrgb \highRgbColor {#4!#6}%
\expanded{\noexpand\cellcolor[rgb]{\ApplyProfile delim, lch \sRGBprofile 2 \lowRgbColor \space \pgfmathresult \highRgbColor}}%
\ifdimcomp{\ApplyProfile \lightnessProfile 2 \lowRgbColor \space \pgfmathresult \highRgbColor pt}{<}{.5pt} {%
\color{white}%
}{%
\color{black}%
}%
#1
}}%
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment