summaryrefslogtreecommitdiff
path: root/source/fitz/svg-device.c
diff options
context:
space:
mode:
authorTor Andersson <tor.andersson@artifex.com>2017-03-08 14:13:39 +0100
committerTor Andersson <tor.andersson@artifex.com>2017-03-15 09:47:01 +0100
commit3071d7cfecab40f0779bb11e33b54cc8b9a613c2 (patch)
treeb044fc89c356050644d4e162ffcb9b9eda6aee04 /source/fitz/svg-device.c
parente310747e00e9146d7ca36e5af9d168438c72d280 (diff)
downloadmupdf-3071d7cfecab40f0779bb11e33b54cc8b9a613c2.tar.xz
svgwrite: Use #RRGGBB color syntax.
Diffstat (limited to 'source/fitz/svg-device.c')
-rw-r--r--source/fitz/svg-device.c56
1 files changed, 26 insertions, 30 deletions
diff --git a/source/fitz/svg-device.c b/source/fitz/svg-device.c
index 3cae86fe..c0085bc8 100644
--- a/source/fitz/svg-device.c
+++ b/source/fitz/svg-device.c
@@ -196,28 +196,37 @@ svg_dev_stroke_state(fz_context *ctx, svg_device *sdev, const fz_stroke_state *s
(stroke_state->linejoin == FZ_LINEJOIN_ROUND ? "round" : "miter")));
}
+static unsigned int
+svg_hex_color(fz_context *ctx, fz_colorspace *colorspace, const float *color)
+{
+ float rgb[3];
+ int r, g, b;
+
+ if (colorspace != fz_device_rgb(ctx))
+ {
+ fz_convert_color(ctx, fz_device_rgb(ctx), rgb, colorspace, color);
+ color = rgb;
+ }
+
+ r = fz_clampi(255 * color[0] + 0.5f, 0, 255);
+ g = fz_clampi(255 * color[1] + 0.5f, 0, 255);
+ b = fz_clampi(255 * color[2] + 0.5f, 0, 255);
+
+ return (r << 16) | (g << 8) | b;
+}
+
static void
svg_dev_fill_color(fz_context *ctx, svg_device *sdev, fz_colorspace *colorspace, const float *color, float alpha)
{
fz_output *out = sdev->out;
- float rgb[FZ_MAX_COLORS];
-
if (colorspace)
{
- if (colorspace != fz_device_rgb(ctx))
- {
- /* If it's not rgb, make it rgb */
- fz_convert_color(ctx, fz_device_rgb(ctx), rgb, colorspace, color);
- color = rgb;
- }
-
- if (color[0] == 0 && color[1] == 0 && color[2] == 0)
- {
- /* don't send a fill, as it will be assumed to be black */
- }
- else
- fz_printf(ctx, out, " fill=\"rgb(%d,%d,%d)\"", (int)(255*color[0] + 0.5), (int)(255*color[1] + 0.5), (int)(255*color[2]+0.5));
+ int rgb = svg_hex_color(ctx, colorspace, color);
+ if (rgb != 0) /* black is the default value */
+ fz_printf(ctx, out, " fill=\"#%06x\"", rgb);
}
+ else
+ fz_printf(ctx, out, " fill=\"none\"");
if (alpha != 1)
fz_printf(ctx, out, " fill-opacity=\"%g\"", alpha);
}
@@ -226,23 +235,10 @@ static void
svg_dev_stroke_color(fz_context *ctx, svg_device *sdev, fz_colorspace *colorspace, const float *color, float alpha)
{
fz_output *out = sdev->out;
- float rgb[FZ_MAX_COLORS];
-
if (colorspace)
- {
- if (colorspace != fz_device_rgb(ctx))
- {
- /* If it's not rgb, make it rgb */
- fz_convert_color(ctx, fz_device_rgb(ctx), rgb, colorspace, color);
- color = rgb;
- }
- fz_printf(ctx, out, " fill=\"none\" stroke=\"rgb(%d,%d,%d)\"", (int)(255*color[0] + 0.5), (int)(255*color[1] + 0.5), (int)(255*color[2]+0.5));
- }
+ fz_printf(ctx, out, " fill=\"none\" stroke=\"#%06x\"", svg_hex_color(ctx, colorspace, color));
else
- {
- /* don't send a stroke, as the color will be assumed to be none */
- fz_printf(ctx, out, " fill=\"none\"");
- }
+ fz_printf(ctx, out, " fill=\"none\" stroke=\"none\"");
if (alpha != 1)
fz_printf(ctx, out, " stroke-opacity=\"%g\"", alpha);
}