Česky
Kamil Dudka

Non-Uniform CA Designer (C++, GAlib, Boost)

File detail

Name:DownloadColor.cpp [Download]
Location: nucad > src
Size:3.2 KB
Last modification:2022-09-09 13:06

Source code

/*
 * Copyright (C) 2009 Kamil Dudka <xdudka00@stud.fit.vutbr.cz>
 *
 * This file is part of nucad (Non-Uniform CA Designer).
 *
 * nucad 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
 * any later version.
 *
 * nucad 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 nucad.  If not, see <http://www.gnu.org/licenses/>.
 */
 
/**
 * @file Color.cpp
 * Implementation of classes Color and FixedFloat
 */
 
#include "Color.h"
 
#include <iomanip>
 
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Color implementation
bool Color::useColors = false;
#ifndef BUILDING_DOX
struct Color::Private {
    EColor            color;
};
#endif
 
Color::Color(EColor color):
    d(new Private)
{
    d->color = color;
}
 
Color::Color(const Color &cObj):
    d(new Private)
{
    d->color = cObj.d->color;
}
 
Color::~Color() {
    delete d;
}
 
void Color::enable(bool b) {
    Color::useColors = b;
}
 
bool Color::isEnabled() {
    return Color::useColors;
}
 
std::ostream& operator<< (std::ostream &stream, const Color &cObj) {
    if (Color::useColors) {
        static const char ESC = '\033';
        stream << ESC;
        switch (cObj.d->color) {
            case C_NO_COLOR:     stream << "[0m";    break;
            case C_BLUE:         stream << "[0;34m"; break;
            case C_GREEN:        stream << "[0;32m"; break;
            case C_CYAN:         stream << "[0;36m"; break;
            case C_RED:          stream << "[0;31m"; break;
            case C_PURPLE:       stream << "[0;35m"; break;
            case C_BROWN:        stream << "[0;33m"; break;
            case C_LIGHT_GRAY:   stream << "[0;37m"; break;
            case C_DARK_GRAY:    stream << "[1;30m"; break;
            case C_LIGHT_BLUE:   stream << "[1;34m"; break;
            case C_LIGHT_GREEN:  stream << "[1;32m"; break;
            case C_LIGHT_CYAN:   stream << "[1;36m"; break;
            case C_LIGHT_RED:    stream << "[1;31m"; break;
            case C_LIGHT_PURPLE: stream << "[1;35m"; break;
            case C_YELLOW:       stream << "[1;33m"; break;
            case C_WHITE:        stream << "[1;37m"; break;
        }
    }
    return stream;
}
 
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// FixedFloat implementation
#ifndef BUILDING_DOX
struct FixedFloat::Private {
    int width;
    int precision;
};
#endif
 
FixedFloat::FixedFloat(int integral, int decimal):
    d(new Private)
{
    d->width = integral+decimal+1;
    d->precision = decimal;
}
 
FixedFloat::~FixedFloat() {
    delete d;
}
 
std::ostream& operator<< (std::ostream &stream, const FixedFloat &manip) {
    return stream
        << std::fixed
        << std::setw(manip.d->width)
        << std::setprecision(manip.d->precision);
}