Blame Scripts/Bash/Functions/Render/Svg/svg_getColors.sh

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