Skip to content

Commit

Permalink
Merge pull request sass#530 from xzyfer/fix/issue_528
Browse files Browse the repository at this point in the history
Remove unnecessary whitespace in rbg/rgba compressed output
  • Loading branch information
HamptonMakes committed Oct 12, 2014
2 parents dc78a68 + 5f6227e commit a0cacf3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
48 changes: 48 additions & 0 deletions output_compressed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "context.hpp"
#include "to_string.hpp"
#include "util.hpp"
#include <cmath>
#include <iomanip>

namespace Sass {
using namespace std;
Expand Down Expand Up @@ -225,6 +227,52 @@ namespace Sass {
}
}

// helper function for serializing colors
template <size_t range>
static double cap_channel(double c) {
if (c > range) return range;
else if (c < 0) return 0;
else return c;
}

void Output_Compressed::operator()(Color* c)
{
stringstream ss;
double r = round(cap_channel<0xff>(c->r()));
double g = round(cap_channel<0xff>(c->g()));
double b = round(cap_channel<0xff>(c->b()));
double a = cap_channel<1> (c->a());

// retain the originally specified color definition if unchanged
if (!c->disp().empty()) {
ss << c->disp();
}
else if (a >= 1) {
// see if it's a named color
int numval = r * 0x10000;
numval += g * 0x100;
numval += b;
if (ctx && ctx->colors_to_names.count(numval)) {
ss << ctx->colors_to_names[numval];
}
else {
// otherwise output the hex triplet
ss << '#' << setw(2) << setfill('0');
ss << hex << setw(2) << static_cast<unsigned long>(r);
ss << hex << setw(2) << static_cast<unsigned long>(g);
ss << hex << setw(2) << static_cast<unsigned long>(b);
}
}
else {
ss << "rgba(";
ss << static_cast<unsigned long>(r) << ",";
ss << static_cast<unsigned long>(g) << ",";
ss << static_cast<unsigned long>(b) << ",";
ss << a << ')';
}
append_singleline_part_to_buffer(ss.str());
}

void Output_Compressed::operator()(Media_Query_Expression* mqe)
{
if (mqe->is_interpolated()) {
Expand Down
2 changes: 1 addition & 1 deletion output_compressed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace Sass {
// virtual void operator()(Variable*);
// virtual void operator()(Textual*);
// virtual void operator()(Number*);
// virtual void operator()(Color*);
virtual void operator()(Color*);
// virtual void operator()(Boolean*);
// virtual void operator()(String_Schema*);
// virtual void operator()(String_Constant* x);
Expand Down

0 comments on commit a0cacf3

Please sign in to comment.