Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Sass::round function #1688

Merged
merged 1 commit into from
Nov 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1864,17 +1864,17 @@ namespace Sass {
// resolved color
std::string res_name = name;

double r = round(cap_channel<0xff>(r_));
double g = round(cap_channel<0xff>(g_));
double b = round(cap_channel<0xff>(b_));
double r = Sass::round(cap_channel<0xff>(r_));
double g = Sass::round(cap_channel<0xff>(g_));
double b = Sass::round(cap_channel<0xff>(b_));
double a = cap_channel<1> (a_);

// get color from given name (if one was given at all)
if (name != "" && name_to_color(name)) {
const Color* n = name_to_color(name);
r = round(cap_channel<0xff>(n->r()));
g = round(cap_channel<0xff>(n->g()));
b = round(cap_channel<0xff>(n->b()));
r = Sass::round(cap_channel<0xff>(n->r()));
g = Sass::round(cap_channel<0xff>(n->g()));
b = Sass::round(cap_channel<0xff>(n->b()));
a = cap_channel<1> (n->a());
}
// otherwise get the possible resolved color name
Expand Down
17 changes: 9 additions & 8 deletions src/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "util.hpp"
#include "expand.hpp"
#include "utf8_string.hpp"
#include "sass/base.h"
#include "utf8.h"

#include <cstdlib>
Expand Down Expand Up @@ -310,9 +311,9 @@ namespace Sass {

return SASS_MEMORY_NEW(ctx.mem, Color,
pstate,
std::round(w1*color1->r() + w2*color2->r()),
std::round(w1*color1->g() + w2*color2->g()),
std::round(w1*color1->b() + w2*color2->b()),
Sass::round(w1*color1->r() + w2*color2->r()),
Sass::round(w1*color1->g() + w2*color2->g()),
Sass::round(w1*color1->b() + w2*color2->b()),
color1->a()*p + color2->a()*(1-p));
}

Expand Down Expand Up @@ -853,10 +854,10 @@ namespace Sass {

std::stringstream ss;
ss << '#' << std::setw(2) << std::setfill('0');
ss << std::hex << std::setw(2) << static_cast<unsigned long>(std::floor(a+0.5));
ss << std::hex << std::setw(2) << static_cast<unsigned long>(std::floor(r+0.5));
ss << std::hex << std::setw(2) << static_cast<unsigned long>(std::floor(g+0.5));
ss << std::hex << std::setw(2) << static_cast<unsigned long>(std::floor(b+0.5));
ss << std::hex << std::setw(2) << static_cast<unsigned long>(Sass::round(a));
ss << std::hex << std::setw(2) << static_cast<unsigned long>(Sass::round(r));
ss << std::hex << std::setw(2) << static_cast<unsigned long>(Sass::round(g));
ss << std::hex << std::setw(2) << static_cast<unsigned long>(Sass::round(b));

std::string result(ss.str());
for (size_t i = 0, L = result.length(); i < L; ++i) {
Expand Down Expand Up @@ -1087,7 +1088,7 @@ namespace Sass {
Number* n = ARG("$number", Number);
Number* r = SASS_MEMORY_NEW(ctx.mem, Number, *n);
r->pstate(pstate);
r->value(std::floor(r->value() + 0.5));
r->value(Sass::round(r->value()));
return r;
}

Expand Down
18 changes: 18 additions & 0 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "constants.hpp"
#include "utf8/checked.h"

#include <cmath>
#include <stdint.h>

namespace Sass {
Expand All @@ -15,6 +16,23 @@ namespace Sass {
exit(EXIT_FAILURE); \
} while (0)

double round(double val)
{
// work around some compiler issue
// cygwin has it not defined in std
using namespace std;

// This was later repatched in 3.4.20
// which is as yet unreleased.
// https://github.com/sass/sass/commit/4e3e1d5684cc29073a507578fc977434ff488c93
if (fmod(val, 1) - 0.5 > -0.00001) return std::ceil(val);
return ::round(val);

// Use this version once sass-spec is at 3.4.20
// if (fmod(val, 1) - 0.5 > -0.00001) return ::round(val);
// return value > 0 ? std::ceil(val) : std::floor(val);
}

/* Sadly, sass_strdup is not portable. */
char *sass_strdup(const char *str)
{
Expand Down
1 change: 1 addition & 0 deletions src/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Sass {

double round(double val);
char* sass_strdup(const char* str);
double sass_atof(const char* str);
const char* safe_str(const char *, const char* = "");
Expand Down