Blame Automation/Modules/Render/Svg/svg_getColors.sh

Alain Reguera Delgado 8f60cb
#!/bin/bash
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# svg_getColors.sh -- This function takes one palette produced by Gimp
Alain Reguera Delgado 8f60cb
# (e.g., syslinux.gpl) as input and outputs a list of colors in the
Alain Reguera Delgado 8f60cb
# specified format. In order for this function to output the color in
Alain Reguera Delgado 8f60cb
# the format specified, it is needed that the fourth column in the gpl
Alain Reguera Delgado 8f60cb
# palette be set in the `rrggbb' format and the appropriate conversion
Alain Reguera Delgado 8f60cb
# be implemented here.
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# Notice that using both the `--head' and `--tail' options it is
Alain Reguera Delgado 8f60cb
# possible to control how many consecutive items does the list of
Alain Reguera Delgado 8f60cb
# colors is going to have.  It is possible to output all colors in the
Alain Reguera Delgado 8f60cb
# list, or a consecutive range of them.
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# Copyright (C) 2009-2013 The CentOS Project
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# This program is free software; you can redistribute it and/or modify
Alain Reguera Delgado 8f60cb
# it under the terms of the GNU General Public License as published by
Alain Reguera Delgado 8f60cb
# the Free Software Foundation; either version 2 of the License, or (at
Alain Reguera Delgado 8f60cb
# your option) any later version.
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# This program is distributed in the hope that it will be useful, but
Alain Reguera Delgado 8f60cb
# WITHOUT ANY WARRANTY; without even the implied warranty of
Alain Reguera Delgado 8f60cb
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Alain Reguera Delgado 8f60cb
# General Public License for more details.
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# You should have received a copy of the GNU General Public License
Alain Reguera Delgado 8f60cb
# along with this program; if not, write to the Free Software
Alain Reguera Delgado 8f60cb
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# ----------------------------------------------------------------------
Alain Reguera Delgado 8f60cb
# $Id$
Alain Reguera Delgado 8f60cb
# ----------------------------------------------------------------------
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
function svg_getColors {
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define short options.
Alain Reguera Delgado 8f60cb
    local ARGSS=''
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define long options.
Alain Reguera Delgado 8f60cb
    local ARGSL='head:,tail:,format:'
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Initialize both head and tail values to return the first line of
Alain Reguera Delgado 8f60cb
    # color information from the palette.
Alain Reguera Delgado 8f60cb
    local HEAD=1
Alain Reguera Delgado 8f60cb
    local TAIL=1
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Initialize format value used as default when no format option be
Alain Reguera Delgado 8f60cb
    # provided.
Alain Reguera Delgado 8f60cb
    local FORMAT='rrggbb'
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Initialize list of colors.
Alain Reguera Delgado 8f60cb
    local COLORS=''
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Initialize arguments with an empty value and set it as local
Alain Reguera Delgado 8f60cb
    # variable to this function scope. Doing this is very important to
Alain Reguera Delgado 8f60cb
    # avoid any clash with higher execution environments.
Alain Reguera Delgado 8f60cb
    local ARGUMENTS=''
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Prepare ARGUMENTS variable for getopt.
Alain Reguera Delgado 8f60cb
    cli_parseArgumentsReDef "$@"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Redefine ARGUMENTS using getopt(1) command parser.
Alain Reguera Delgado 8f60cb
    cli_parseArguments
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Redefine positional parameters using ARGUMENTS variable.
Alain Reguera Delgado 8f60cb
    eval set -- "$ARGUMENTS"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Look for options passed through positional parameters.
Alain Reguera Delgado 8f60cb
    while true;do
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        case "$1" in 
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            --head )
Alain Reguera Delgado 8f60cb
                HEAD=$2
Alain Reguera Delgado 8f60cb
                shift 2
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            --tail )
Alain Reguera Delgado 8f60cb
                TAIL=$2
Alain Reguera Delgado 8f60cb
                shift 2
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            --format )
Alain Reguera Delgado 8f60cb
                FORMAT=$2
Alain Reguera Delgado 8f60cb
                shift 2
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            -- )
Alain Reguera Delgado 8f60cb
                shift 1
Alain Reguera Delgado 8f60cb
                break
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
        esac
Alain Reguera Delgado 8f60cb
    done
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define path to gpl palette. This is the first file we use to
Alain Reguera Delgado 8f60cb
    # retrieve color information from. Only the first file provided
Alain Reguera Delgado 8f60cb
    # will be used.
Alain Reguera Delgado 8f60cb
    local PALETTE=$(echo $@ | cut -d' ' -f1)
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    if [[ ! -f $PALETTE ]];then
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        # Define palette path inside the theme's artistic motif.
Alain Reguera Delgado 8f60cb
        local MOTIF_PALETTE=${TCAR_WORKDIR}/Identity/Images/Themes/$(cli_getPathComponent $ACTIONVAL --motif)/Palettes/grub.gpl
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        # Define palette path inside the theme's design model.
Alain Reguera Delgado 8f60cb
        local MODEL_PALETTE=${TCAR_WORKDIR}/Identity/Models/Themes/${THEME_MODEL_NAME}/Palettes/grub.gpl
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        # Redefine default background color using palettes provided by
Alain Reguera Delgado 8f60cb
        # artistic motif first, and design model later. Assuming none
Alain Reguera Delgado 8f60cb
        # of them is present, use The CentOS Project default color
Alain Reguera Delgado 8f60cb
        # then.
Alain Reguera Delgado 8f60cb
        if [[ -f $MOTIF_PALETTE ]];then
Alain Reguera Delgado 8f60cb
            COLORS=$(svg_getColors $MOTIF_PALETTE --head=1 --tail=1)
Alain Reguera Delgado 8f60cb
        elif [[ -f $MODEL_PALETTE ]];then
Alain Reguera Delgado 8f60cb
            COLORS=$(svg_getColors $MODEL_PALETTE --head=1 --tail=1)
Alain Reguera Delgado 8f60cb
        else
Alain Reguera Delgado 8f60cb
            COLORS='#204c8d'
Alain Reguera Delgado 8f60cb
        fi
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    else
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        # Retrieve the fourth column from GPL palette. The fourth
Alain Reguera Delgado 8f60cb
        # column of a GPL palette contains the palette commentary
Alain Reguera Delgado 8f60cb
        # field. The palette commentary field can be anything, but for
Alain Reguera Delgado 8f60cb
        # the sake of our own convenience we use it to store the color
Alain Reguera Delgado 8f60cb
        # value in hexadecimal format (e.g., rrggbb).  Notice that you
Alain Reguera Delgado 8f60cb
        # can put your comments from the fifth column on using an
Alain Reguera Delgado 8f60cb
        # space as field separator.
Alain Reguera Delgado 8f60cb
        COLORS=$(sed -r '1,/^#/d' $PALETTE \
Alain Reguera Delgado 8f60cb
            | awk '{ printf "%s\n", $4 }' | head -n $HEAD | tail -n $TAIL)
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    fi
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Implement color formats conversions from rrggbb to other formats
Alain Reguera Delgado 8f60cb
    # that you might need to use.
Alain Reguera Delgado 8f60cb
    for COLOR in $COLORS;do
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        case $FORMAT in
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            rrggbb|* )
Alain Reguera Delgado 8f60cb
                if [[ ! $COLOR =~ '^#' ]];then
Alain Reguera Delgado 8f60cb
                    COLOR="#${COLOR}"
Alain Reguera Delgado 8f60cb
                fi
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        esac
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        # Output color value.
Alain Reguera Delgado 8f60cb
        echo "$COLOR"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    done
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
}