tools.R 9.20 KiB
# \\\
# Copyright 2021-2022 Louis Héraut*1
# *1   INRAE, France
#      louis.heraut@inrae.fr
# This file is part of ash R toolbox.
# ash R toolbox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# ash R toolbox is distributed in the hope that it will be useful, but 
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with ash R toolbox.  If not, see <https://www.gnu.org/licenses/>.
# ///
# tools.R
## 1. COLOR MANAGEMENT
### 1.1. Color on colorbar ___________________________________________
# Returns a color of a palette corresponding to a value included
# between the min and the max of the variable
get_color = function (value, min, max, ncolor=256, palette_name='perso', reverse=FALSE) {
    # If the value is a NA return NA color
    if (is.na(value)) {
        return (NA)
    # If the palette chosen is the personal ones
    if (palette_name == 'perso') {
        colorList = palette_perso
    # Else takes the palette corresponding to the name given
    } else {
        colorList = brewer.pal(11, palette_name)
    # Gets the number of discrete colors in the palette
    nSample = length(colorList)
    # Recreates a continuous color palette
    palette = colorRampPalette(colorList)(ncolor)
    # Separates it in the middle to have a cold and a hot palette
    Sample_hot = 1:(as.integer(nSample/2)+1)
    Sample_cold = (as.integer(nSample/2)+1):nSample
    palette_hot = colorRampPalette(colorList[Sample_hot])(ncolor)
    palette_cold = colorRampPalette(colorList[Sample_cold])(ncolor)
    # Reverses the palette if it needs to be
    if (reverse) {
        palette = rev(palette)
        palette_hot = rev(palette_hot)
        palette_cold = rev(palette_cold)
    # Computes the absolute max
    maxAbs = max(abs(max), abs(min))
    # If the value is negative
    if (value < 0) {
        if (maxAbs == 0) {
            idNorm = 0
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
} else { # Gets the relative position of the value in respect # to its span idNorm = (value + maxAbs) / maxAbs } # The index corresponding id = round(idNorm*(ncolor - 1) + 1, 0) # The associated color color = palette_cold[id] # Same if it is a positive value } else { if (maxAbs == 0) { idNorm = 0 } else { idNorm = value / maxAbs } id = round(idNorm*(ncolor - 1) + 1, 0) color = palette_hot[id] } return(color) } ### 1.2. Colorbar ____________________________________________________ # Returns the colorbar but also positions, labels and colors of some # ticks along it get_palette = function (min, max, ncolor=256, palette_name='perso', reverse=FALSE, nbTick=10) { # If the value is a NA return NA color if (is.null(min) | is.null(max)) { return (NA) } # If the palette chosen is the personal ones if (palette_name == 'perso') { colorList = palette_perso # Else takes the palette corresponding to the name given } else { colorList = brewer.pal(11, palette_name) } # Gets the number of discrete colors in the palette nSample = length(colorList) # Recreates a continuous color palette palette = colorRampPalette(colorList)(ncolor) # Separates it in the middle to have a cold and a hot palette Sample_hot = 1:(as.integer(nSample/2)+1) Sample_cold = (as.integer(nSample/2)+1):nSample palette_hot = colorRampPalette(colorList[Sample_hot])(ncolor) palette_cold = colorRampPalette(colorList[Sample_cold])(ncolor) # Reverses the palette if it needs to be if (reverse) { palette = rev(palette) palette_hot = rev(palette_hot) palette_cold = rev(palette_cold) } # If the min and the max are below zero if (min < 0 & max < 0) { # The palette show is only the cold one paletteShow = palette_cold # If the min and the max are above zero } else if (min > 0 & max > 0) { # The palette show is only the hot one paletteShow = palette_hot # Else it is the entire palette that is shown } else { paletteShow = palette }