/* === S Y N F I G ========================================================= */
-/*! \file template.cpp
+/*! \file palette.cpp
** \brief Template File
**
-** $Id: palette.cpp,v 1.1.1.1 2005/01/04 01:23:14 darco Exp $
+** $Id$
**
** \legal
-** Copyright (c) 2002 Robert B. Quattlebaum Jr.
+** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
**
-** This software and associated documentation
-** are CONFIDENTIAL and PROPRIETARY property of
-** the above-mentioned copyright holder.
+** This package 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 of
+** the License, or (at your option) any later version.
**
-** You may not copy, print, publish, or in any
-** other way distribute this software without
-** a prior written agreement with
-** the copyright holder.
+** This package 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.
** \endlegal
*/
/* ========================================================================= */
Palette::Palette(const Surface& surface, int max_colors):
name_(_("Surface Palette"))
-{
+{
max_colors-=2;
- for(int y=0;y<surface.get_h();y++)
- for(int x=0;x<surface.get_w();x++)
- {
+ for(int i=0;(signed)size()<(max_colors-1) && i<max_colors*16;++i) {
+ int x=rand()%surface.get_w();
+ int y=rand()%surface.get_h();
+
float dist;
Color color(surface[y][x]);
-
+
if(empty())
{
push_back(color);
iter->add(color);
continue;
}
-
+
/*if(size()>=max_colors)
{
iterator iterlight(find_light());
find_closest(light.color)->add(light.color,light.weight);
}
*/
-
+
push_back(color);
- continue;
+ continue;
+ }
+
+/*
+
+ max_colors-=2;
+ for(int y=0;y<surface.get_h();y++)
+ for(int x=0;x<surface.get_w();x++)
+ {
+ float dist;
+ Color color(surface[y][x]);
+
+ if(empty())
+ {
+ push_back(color);
+ continue;
+ }
+
+ if(color.get_a()==0)
+ {
+ if(front().color.get_a()!=0)
+ insert(begin(),Color(1,0,1,0));
+ front().weight+=400;
+ continue;
+ }
+
+ iterator iter(find_closest(color,&dist));
+ if(sqrt(dist)<0.005)
+ {
+ iter->add(color);
+ continue;
+ }
+
+
+ push_back(color);
+ continue;
}
sort(rbegin(),rend());
pop_back();
find_closest(item.color)->add(item.color,item.weight);
}
+*/
push_back(Color::black());
push_back(Color::white());
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());
diff_y*diff_y*1.5f+
diff_a*diff_a+
- diff_u*diff_u+
+ diff_u*diff_u+
diff_v*diff_v
-
+
// cross product
/*abs(
- prep_u*iter->color.get_u()-
+ prep_u*iter->color.get_u()-
prep_v*iter->color.get_v()
)*/
);
}
if(dist)
*dist=best_dist;
-
+
return best_match;
}
iterator iter;
iterator best_match(begin());
-
+
for(iter=begin();iter!=end();++iter)
{
if(iter->weight>best_match->weight)
best_match=iter;
}
-
+
return best_match;
}
iterator iter;
iterator best_match(begin());
-
+
for(iter=begin();iter!=end();++iter)
{
if(iter->weight<best_match->weight)
best_match=iter;
}
-
+
return best_match;
}
const_iterator iter;
std::ofstream file(filename.c_str());
-
+
if(!file)
throw strprintf(_("Unable to open %s for write"),filename.c_str());
-
+
file<<PALETTE_FILE_COOKIE<<endl;
- file<<name_<<endl;
+ file<<name_<<endl;
for(iter=begin();iter!=end();++iter)
{
file<<iter->name<<endl;
<<iter->color.get_g()<<endl
<<iter->color.get_b()<<endl
<<iter->color.get_a()<<endl;
-
+
}
}
Palette::load_from_file(const synfig::String& filename)
{
std::ifstream file(filename.c_str());
-
+
if(!file)
throw strprintf(_("Unable to open %s for read"),filename.c_str());
-
+
Palette ret;
String line;
-
+
getline(file,line);
if(line!=PALETTE_FILE_COOKIE)
throw strprintf(_("%s does not appear to be a palette file"),filename.c_str());
getline(file,ret.name_);
-
+
while(!file.eof())
{
PaletteItem item;
getline(file,line);
if(!file.eof())break;
item.color.set_a(atof(line.c_str()));
-
+
ret.push_back(item);
}
-
+
return ret;
}