/* * Copyright (C) 2004-2005 Vadim Berezniker * http://www.kryptolus.com * * This Program 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 2, or (at your option) * any later version. * * This Program 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 GNU Make; see the file COPYING. If not, write to * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. * http://www.gnu.org/copyleft/gpl.html * */ #include "stdafx.h" #include "common.h" #include "sabbu.h" extern struct sabbu app; /* * This object represents a color associated with a something on the User Interface. * (The main purpose of which is to allow the user to customize the interface) * * See the Set method for information on the properties contained by this object. */ /* * Creates a new color with no color data. * Before it can be used, the color must be initialized via the Set method call. */ kryColor::kryColor() : kryObject(kryColor::SIGNAL_COUNT) { this->m_initialized = FALSE; this->m_user_data = NULL; this->m_opacity = FALSE; } /* * Creates a new color and initializes the color data to the given information. * * See the Set method for parameter information. */ kryColor::kryColor(int id, char *t_id, char *name, int red, int green, int blue) : kryObject(kryColor::SIGNAL_COUNT) { this->m_opacity = FALSE; this->Set(id, t_id, name, red, green, blue); } /* * Initializes the color data to the given information. * * id: Unique Numeric ID for the color. * t_id: Unique Text ID for the color. * name: Name of the interface item with which this color is associated with. * red: Red intensity of the color (0 - 255) * green: Green intensity of the color (0 - 255) * blue: Blue intensity of the color (0 - 255) */ void kryColor::Set(int id, char *t_id, char *name, int red, int green, int blue) { /* Check for sensible values */ red = sabbu_clamp_value(red, 0, 255); green = sabbu_clamp_value(green, 0, 255); blue = sabbu_clamp_value(blue, 0, 255); /* If this is the first color data assigned to this color, we remember it so that the color can be reset again to the initial value if necessary */ if(!this->IsInitialized()) { this->m_orig_alpha = 255; this->m_orig_red = red; this->m_orig_green = green; this->m_orig_blue = blue; } this->m_id = id; this->m_text_id = t_id; this->m_name = name; this->m_alpha = 255; this->m_initialized = TRUE; this->m_user_data = NULL; this->m_red = red; this->m_green = green; this->m_blue = blue; } /* * Restores the color data to the original values. */ void kryColor::Reset() { if(!this->IsInitialized()) { g_warning("Trying to reset a color that has not be initialized. Bailing out."); return; } this->m_red = this->m_orig_red; this->m_green = this->m_orig_green; this->m_blue = this->m_orig_blue; this->m_alpha = this->m_orig_alpha; // freaking terrible hack. // the proper way to do this is simply to have the color table talk to the prefs, not through a 3rd party char *val = kry_strdup_printf(KRY_LOC "%d", this->GetRed() << 16 | this->GetGreen() << 8 | this->GetBlue()); if(this->GetTextID()) app.prefs->SetString("Colors", this->GetTextID(), val); kry_free(val); /* Notify about data change */ this->InvokeSignal(kryColor::SIGNAL_CHANGED, NULL); } /* * Returns the stored pointer within the object. */ void *kryColor::GetUserData() { return this->m_user_data; } /* * Stores the given pointer within the object. */ void kryColor::SetUserData(void *data) { this->m_user_data = data; } /* * Returns the Red intensity of the color (0 - 255). * * The color must have already been initialized. */ int kryColor::GetRed() { if(!this->IsInitialized()) { g_warning("Trying to obtain Red intensity of an uninitialized color. "); return 0; } return this->m_red; } /* * Returns the Green intensity of the color (0 - 255). * * The color must have already been initialized. */ int kryColor::GetGreen() { if(!this->IsInitialized()) { g_warning("Trying to obtain Green intensity of an uninitialized color. "); return 0; } return this->m_green; } /* * Returns the Blue intensity of the color (0 - 255). * * The color must have already been initialized. */ int kryColor::GetBlue() { if(!this->IsInitialized()) { g_warning("Trying to obtain Blue intensity of an uninitialized color. "); return 0; } return this->m_blue; } /* * Returns the display Name of the color (0 - 255). * * The color must have already been initialized. */ char *kryColor::GetName() { if(!this->IsInitialized()) { g_warning("Trying to obtain the Name of an uninitialized color. "); return NULL; } return this->m_name; } /* * Sets the color information to the given intensities. * This function can only be used after the color has already been initialized. * (either via the constructor or the Set method). * * The intensity of each channel must be between 0 and 255. */ void kryColor::SetColor(int red, int green, int blue) { if(!this->IsInitialized()) { g_warning("Trying to set color on an uninitialized color. "); return; } if(this->m_red == red && this->m_green == green && this->m_blue == blue) return; /* Check for sensible values */ this->m_red = sabbu_clamp_value(red, 0, 255); this->m_green = sabbu_clamp_value(green, 0, 255); this->m_blue = sabbu_clamp_value(blue, 0, 255); /* Notify about color change */ this->InvokeSignal(kryColor::SIGNAL_CHANGED, NULL); } /* * Returns whether or not the color data of this color has been initialized. */ gboolean kryColor::IsInitialized() { return this->m_initialized; } /* * Returns the numeric ID of this color. * * The color must have already been initialized. */ int kryColor::GetID() { if(!this->IsInitialized()) { g_warning("Trying to obtain numeric ID of an uninitialized color. "); return 0; } return this->m_id; } /* * Returns the text ID of this color. * * The color must have already been initialized. */ char *kryColor::GetTextID() { if(!this->IsInitialized()) { g_warning("Trying to obtain text ID of an uninitialized color. "); return NULL; } return this->m_text_id; } /* * Enables the opacity channel for this color. * * The color must have already been initialized. */ void kryColor::EnableOpacity() { if(!this->IsInitialized()) { g_warning("Trying to enable opacity on an uninitialized color. "); return; } this->m_opacity = TRUE; } /* * Disables the opacity channel for this color. * * The color must have already been initialized. */ void kryColor::DisableOpacity() { if(!this->IsInitialized()) { g_warning("Trying to enable opacity on an uninitialized color. "); return; } this->m_opacity = FALSE; } /* * Returns whether or not the opacity channel is enabled for this color. * * The color must have already been initialized. */ gboolean kryColor::HasOpacity() { if(!this->IsInitialized()) { g_warning("Trying to obtain Opacity status of an uninitialized color. "); return FALSE; } return this->m_opacity; } /* * Sets the Alpha value of this color. (0 - 255) * * The color must have already been initialized and opacity must have been enabled. */ void kryColor::SetAlpha(int alpha) { if(!this->IsInitialized()) { g_warning("Trying to set Alpha value of an uninitialized color. "); return; } this->m_alpha = sabbu_clamp_value(alpha, 0, 255); } /* * Returns the text Alpha value of this color. * * The color must have already been initialized and opacity must have been enabled. */ int kryColor::GetAlpha() { if(!this->IsInitialized()) { g_warning("Trying to obtain Alpha value of an uninitialized color. "); return 0; } return this->m_alpha; }