+ acre->chart.width / (acre->x_axis.view_max - acre->x_axis.view_min),
+ - acre->chart.height /(acre->y_axis.view_max - acre->y_axis.view_min));
+ cairo_translate (cr, -acre->x_axis.view_min, -acre->y_axis.view_min);
+}
+
+static void
+_compute_axis_ranges (acre_t *acre)
+{
+ double x_adjust, y_adjust;
+ cairo_t *cr = acre->cr;
+
+ /* First, simply set view ranges to data ranges. */
+ acre->x_axis.view_min = acre->x_axis.data_min;
+ acre->x_axis.view_max = acre->x_axis.data_max;
+
+ acre->y_axis.view_min = acre->y_axis.data_min;
+ acre->y_axis.view_max = acre->y_axis.data_max;
+
+ /* Next, we want to ensure that the data never collides with the
+ * ticks. So we expand each axis on its minimum side as needed. */
+ cairo_save (cr);
+ {
+ double x, y;
+
+ _set_transform_to_data_space (acre);
+
+ x = ACRE_TICK_MAJOR_SIZE + 2.0;
+ y = ACRE_TICK_MAJOR_SIZE + 2.0;
+ cairo_device_to_user_distance (cr, &x, &y);
+
+ acre->x_axis.view_min -= x;
+ acre->y_axis.view_min += y;
+ }
+ cairo_restore (cr);
+
+ /* Then, increase the axis ranges just enough so that the step
+ * sizes for the ticks will be integers.
+ */
+ _expand_range_for_width (&acre->x_axis.view_min,
+ &acre->x_axis.view_max,
+ acre->chart.width);
+
+ _expand_range_for_width (&acre->y_axis.view_min,
+ &acre->y_axis.view_max,
+ acre->chart.height);
+
+ /* Finally, we also translate the axis ranges slightly so that the
+ * ticks land on half-integer device-pixel positions.
+ */
+ cairo_save (cr);
+ {
+ _set_transform_to_data_space (acre);
+
+ x_adjust = 0.0;
+ y_adjust = 0.0;
+ cairo_user_to_device (cr, &x_adjust, &y_adjust);
+ x_adjust = (round (x_adjust + 0.5) - 0.5) - x_adjust;
+ y_adjust = (round (y_adjust + 0.5) - 0.5) - y_adjust;
+ cairo_device_to_user_distance (cr, &x_adjust, &y_adjust);
+
+ acre->x_axis.view_min -= x_adjust;
+ acre->x_axis.view_max -= x_adjust;
+
+ acre->y_axis.view_min -= y_adjust;
+ acre->y_axis.view_max -= y_adjust;
+ }
+ cairo_restore (cr);
+}
+
+static void
+_acre_color_from_hsv (acre_color_t *color,
+ double hue,
+ double saturation,
+ double value)
+{
+ double f, p, q, t;
+ int hmod6;
+
+ hmod6 = (int) floor (hue / 60) % 6;
+ f = hue / 60 - floor (hue / 60);
+ p = value * (1 - saturation);
+ q = value * (1 - f * saturation);
+ t = value * (1 - (1 - f) * saturation);
+
+ switch (hmod6) {
+ case 0:
+ color->red = value;
+ color->green = t;
+ color->blue = p;
+ break;
+ case 1:
+ color->red = q;
+ color->green = value;
+ color->blue = p;
+ break;
+ case 2:
+ color->red = p;
+ color->green = value;
+ color->blue = t;
+ break;
+ case 3:
+ color->red = p;
+ color->green = q;
+ color->blue = value;
+ break;
+ case 4:
+ color->red = t;
+ color->green = p;
+ color->blue = value;
+ break;
+ case 5:
+ color->red = value;
+ color->green = p;
+ color->blue = q;
+ break;
+ }
+}
+
+static void
+_choose_colors (acre_t *acre)
+{
+ cmsHPROFILE lab_profile, srgb_profile;
+ cmsHTRANSFORM lab_to_srgb;
+ int i;
+ double theta, radius, srgb[3];
+ cmsCIELab lab;
+
+ lab_profile = cmsCreateLabProfile (NULL); /* D50 */
+ srgb_profile = cmsCreate_sRGBProfile ();
+
+ lab_to_srgb = cmsCreateTransform (lab_profile, TYPE_Lab_DBL,
+ srgb_profile, TYPE_RGB_DBL,
+ INTENT_PERCEPTUAL, 0);
+
+ acre->num_colors = acre->num_data;
+
+ if (acre->num_colors > acre->colors_size) {
+ acre->colors_size = acre->num_colors;
+ acre->colors = xrealloc (acre->colors,
+ acre->colors_size * sizeof (acre_color_t));
+ }
+
+ lab.L = 36;
+ radius = 130;
+ for (i = 0; i < acre->num_colors; i++) {
+ theta = 0.713 + 2 * M_PI * (double) i / acre->num_colors;
+ lab.a = radius * cos (theta);
+ lab.b = radius * sin (theta);
+
+ cmsDoTransform (lab_to_srgb, &lab, srgb, 1);
+
+ acre->colors[i].red = srgb[0];
+ acre->colors[i].green = srgb[1];
+ acre->colors[i].blue = srgb[2];
+ }
+
+ cmsDeleteTransform (lab_to_srgb);
+ cmsCloseProfile (lab_profile);
+ cmsCloseProfile (srgb_profile);