PageRenderTime 31ms CodeModel.GetById 18ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmath/v3color.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 153 lines | 106 code | 22 blank | 25 comment | 18 complexity | faa42b2831c08883ec35b8e36593b688 MD5 | raw file
  1/** 
  2 * @file v3color.cpp
  3 * @brief LLColor3 class implementation.
  4 *
  5 * $LicenseInfo:firstyear=2000&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#include "linden_common.h"
 28
 29#include "v3color.h"
 30#include "v4color.h"
 31#include "v4math.h"
 32
 33LLColor3 LLColor3::white(1.0f, 1.0f, 1.0f);
 34LLColor3 LLColor3::black(0.0f, 0.0f, 0.0f);
 35LLColor3 LLColor3::grey (0.5f, 0.5f, 0.5f);
 36
 37LLColor3::LLColor3(const LLColor4 &a)
 38{
 39	mV[0] = a.mV[0];
 40	mV[1] = a.mV[1];
 41	mV[2] = a.mV[2];
 42}
 43
 44LLColor3::LLColor3(const LLVector4 &a)
 45{
 46	mV[0] = a.mV[0];
 47	mV[1] = a.mV[1];
 48	mV[2] = a.mV[2];
 49}
 50
 51LLColor3::LLColor3(const LLSD &sd)
 52{
 53	setValue(sd);
 54}
 55
 56const LLColor3& LLColor3::operator=(const LLColor4 &a) 
 57{
 58	mV[0] = a.mV[0];
 59	mV[1] = a.mV[1];
 60	mV[2] = a.mV[2];
 61	return (*this);
 62}
 63
 64std::ostream& operator<<(std::ostream& s, const LLColor3 &a) 
 65{
 66	s << "{ " << a.mV[VX] << ", " << a.mV[VY] << ", " << a.mV[VZ] << " }";
 67	return s;
 68}
 69
 70static F32 hueToRgb ( F32 val1In, F32 val2In, F32 valHUeIn )
 71{
 72	if ( valHUeIn < 0.0f ) valHUeIn += 1.0f;
 73	if ( valHUeIn > 1.0f ) valHUeIn -= 1.0f;
 74	if ( ( 6.0f * valHUeIn ) < 1.0f ) return ( val1In + ( val2In - val1In ) * 6.0f * valHUeIn );
 75	if ( ( 2.0f * valHUeIn ) < 1.0f ) return ( val2In );
 76	if ( ( 3.0f * valHUeIn ) < 2.0f ) return ( val1In + ( val2In - val1In ) * ( ( 2.0f / 3.0f ) - valHUeIn ) * 6.0f );
 77	return ( val1In );
 78}
 79
 80void LLColor3::setHSL ( F32 hValIn, F32 sValIn, F32 lValIn)
 81{
 82	if ( sValIn < 0.00001f )
 83	{
 84		mV[VRED] = lValIn;
 85		mV[VGREEN] = lValIn;
 86		mV[VBLUE] = lValIn;
 87	}
 88	else
 89	{
 90		F32 interVal1;
 91		F32 interVal2;
 92
 93		if ( lValIn < 0.5f )
 94			interVal2 = lValIn * ( 1.0f + sValIn );
 95		else
 96			interVal2 = ( lValIn + sValIn ) - ( sValIn * lValIn );
 97
 98		interVal1 = 2.0f * lValIn - interVal2;
 99
100		mV[VRED] = hueToRgb ( interVal1, interVal2, hValIn + ( 1.f / 3.f ) );
101		mV[VGREEN] = hueToRgb ( interVal1, interVal2, hValIn );
102		mV[VBLUE] = hueToRgb ( interVal1, interVal2, hValIn - ( 1.f / 3.f ) );
103	}
104}
105
106void LLColor3::calcHSL(F32* hue, F32* saturation, F32* luminance) const
107{
108	F32 var_R = mV[VRED];
109	F32 var_G = mV[VGREEN];
110	F32 var_B = mV[VBLUE];
111
112	F32 var_Min = ( var_R < ( var_G < var_B ? var_G : var_B ) ? var_R : ( var_G < var_B ? var_G : var_B ) );
113	F32 var_Max = ( var_R > ( var_G > var_B ? var_G : var_B ) ? var_R : ( var_G > var_B ? var_G : var_B ) );
114
115	F32 del_Max = var_Max - var_Min;
116
117	F32 L = ( var_Max + var_Min ) / 2.0f;
118	F32 H = 0.0f;
119	F32 S = 0.0f;
120
121	if ( del_Max == 0.0f )
122	{
123	   H = 0.0f;
124	   S = 0.0f;
125	}
126	else
127	{
128		if ( L < 0.5 )
129			S = del_Max / ( var_Max + var_Min );
130		else
131			S = del_Max / ( 2.0f - var_Max - var_Min );
132
133		F32 del_R = ( ( ( var_Max - var_R ) / 6.0f ) + ( del_Max / 2.0f ) ) / del_Max;
134		F32 del_G = ( ( ( var_Max - var_G ) / 6.0f ) + ( del_Max / 2.0f ) ) / del_Max;
135		F32 del_B = ( ( ( var_Max - var_B ) / 6.0f ) + ( del_Max / 2.0f ) ) / del_Max;
136
137		if ( var_R >= var_Max )
138			H = del_B - del_G;
139		else
140		if ( var_G >= var_Max )
141			H = ( 1.0f / 3.0f ) + del_R - del_B;
142		else
143		if ( var_B >= var_Max )
144			H = ( 2.0f / 3.0f ) + del_G - del_R;
145
146		if ( H < 0.0f ) H += 1.0f;
147		if ( H > 1.0f ) H -= 1.0f;
148	}
149
150	if (hue) *hue = H;
151	if (saturation) *saturation = S;
152	if (luminance) *luminance = L;
153}