X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=synfig-core%2Ftrunk%2Fsrc%2Fsynfig%2Fpalette.cpp;fp=synfig-core%2Ftrunk%2Fsrc%2Fsynfig%2Fpalette.cpp;h=4ff3a660c5beeea3fa0c1453509c48e7646a92f8;hb=e3acc0b267b14fda5db3c7bbb2f218b993ef84b3;hp=0000000000000000000000000000000000000000;hpb=ced68168d8518aac837f20e73bcd1e84a6bd6178;p=synfig.git diff --git a/synfig-core/trunk/src/synfig/palette.cpp b/synfig-core/trunk/src/synfig/palette.cpp new file mode 100644 index 0000000..4ff3a66 --- /dev/null +++ b/synfig-core/trunk/src/synfig/palette.cpp @@ -0,0 +1,321 @@ +/* === S I N F G =========================================================== */ +/*! \file template.cpp +** \brief Template File +** +** $Id: palette.cpp,v 1.1.1.1 2005/01/04 01:23:14 darco Exp $ +** +** \legal +** Copyright (c) 2002 Robert B. Quattlebaum Jr. +** +** This software and associated documentation +** are CONFIDENTIAL and PROPRIETARY property of +** the above-mentioned copyright holder. +** +** You may not copy, print, publish, or in any +** other way distribute this software without +** a prior written agreement with +** the copyright holder. +** \endlegal +*/ +/* ========================================================================= */ + +/* === H E A D E R S ======================================================= */ + +#ifdef USING_PCH +# include "pch.h" +#else +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "palette.h" +#include "surface.h" +#include "general.h" +#include +#include + +#endif + +/* === U S I N G =========================================================== */ + +using namespace std; +using namespace etl; +using namespace sinfg; + +/* === M A C R O S ========================================================= */ + +#define PALETTE_FILE_COOKIE "SINFGPAL1.0" + +/* === G L O B A L S ======================================================= */ + +bool weight_less_than(const PaletteItem& lhs,const PaletteItem& rhs) +{ + return lhs.weightadd(color); + continue; + } + + /*if(size()>=max_colors) + { + iterator iterlight(find_light()); + PaletteItem light(*iterlight); + erase(iterlight); + find_closest(light.color)->add(light.color,light.weight); + } + */ + + push_back(color); + continue; + } + sort(rbegin(),rend()); + + iterator iter; + + iterator best_match(begin()); + while((signed)size()>max_colors) + { + PaletteItem item(back()); + pop_back(); + find_closest(item.color)->add(item.color,item.weight); + } + push_back(Color::black()); + push_back(Color::white()); + +// sort(begin(),end(),&luma_less_than); +} + +Palette::const_iterator +Palette::find_closest(const Color& color, float* dist)const +{ + // For the sake of avoiding cut-and-paste + // bugs, we'll just use the non-const + // find_closest()... It doesn't change anything + // anyway. + return const_cast(this)->find_closest(color,dist); +} + +Palette::iterator +Palette::find_closest(const Color& color, float* dist) +{ + iterator iter; + + iterator best_match(begin()); + float best_dist(1000000); + + const float prep_y(powf(color.get_y(),2.2f)*color.get_a()); + const float prep_u(color.get_u()); + const float prep_v(color.get_v()); + + for(iter=begin();iter!=end();++iter) + { + const float diff_y(prep_y-powf(iter->color.get_y(),2.2f)*iter->color.get_a()); + const float diff_u(prep_u-iter->color.get_u()); + const float diff_v(prep_v-iter->color.get_v()); + const float diff_a(color.get_a()-iter->color.get_a()); + + + const float dist( + diff_y*diff_y*1.5f+ + diff_a*diff_a+ + + diff_u*diff_u+ + diff_v*diff_v + + // cross product + /*abs( + prep_u*iter->color.get_u()- + prep_v*iter->color.get_v() + )*/ + ); + if(distweight>best_match->weight) + best_match=iter; + } + + return best_match; +} + +Palette::iterator +Palette::find_light() +{ + iterator iter; + + iterator best_match(begin()); + + for(iter=begin();iter!=end();++iter) + { + if(iter->weightweight) + best_match=iter; + } + + return best_match; +} + +Palette +Palette::grayscale(int steps) +{ + Palette ret; + for(int i=0;iname<color.get_r()<color.get_g()<color.get_b()<color.get_a()<