//void fcurve_rainbow (unsigned int cur, unsigned int tot, float *out) void getcolor_fcurve_rainbow(int cur, int tot, float out[3]) { float hsv[3], fac; int grouping; /* we try to divide the color into groupings of n colors, * where n is: * 3 - for 'odd' numbers of curves - there should be a majority of triplets of curves * 4 - for 'even' numbers of curves - there should be a majority of quartets of curves * so the base color is simply one of the three primary colors */ grouping = (4 - (tot % 2)); hsv[0] = HSV_BANDWIDTH * (float)(cur % grouping); /* 'Value' (i.e. darkness) needs to vary so that larger sets of three will be * 'darker' (i.e. smaller value), so that they don't look that similar to previous ones. * However, only a range of 0.3 to 1.0 is really usable to avoid clashing * with some other stuff */ fac = ((float)cur / (float)tot) * 0.7f; /* the base color can get offset a bit so that the colors aren't so identical */ hsv[0] += fac * HSV_BANDWIDTH; if (hsv[0] > 1.0f) hsv[0] = fmod(hsv[0], 1.0f); /* saturation adjustments for more visible range */ hsv[1] = ((hsv[0] > 0.5f) && (hsv[0] < 0.8f)) ? 0.5f : 0.6f; /* value is fixed at 1.0f, otherwise we cannot clearly see the curves... */ hsv[2] = 1.0f; /* finally, conver this to RGB colors */ hsv_to_rgb_v(hsv, out); }
void ConvertHSVToRGBOperation::executePixel(float output[4], float x, float y, PixelSampler sampler) { float inputColor[4]; this->m_inputOperation->read(inputColor, x, y, sampler); hsv_to_rgb_v(inputColor, output); output[3] = inputColor[3]; }
static int Color_channel_hsv_set(ColorObject *self, PyObject *value, void *type) { float hsv[3]; int i = GET_INT_FROM_POINTER(type); float f = PyFloat_AsDouble(value); if (f == -1 && PyErr_Occurred()) { PyErr_SetString(PyExc_TypeError, "color.h/s/v = value: " "assigned value not a number"); return -1; } if (BaseMath_ReadCallback(self) == -1) return -1; rgb_to_hsv_v(self->col, hsv); CLAMP(f, 0.0f, 1.0f); hsv[i] = f; hsv_to_rgb_v(hsv, self->col); if (BaseMath_WriteCallback(self) == -1) return -1; return 0; }
static int Color_hsv_set(ColorObject *self, PyObject *value, void *UNUSED(closure)) { float hsv[3]; if (mathutils_array_parse(hsv, 3, 3, value, "mathutils.Color.hsv = value") == -1) return -1; CLAMP(hsv[0], 0.0f, 1.0f); CLAMP(hsv[1], 0.0f, 1.0f); CLAMP(hsv[2], 0.0f, 1.0f); hsv_to_rgb_v(hsv, self->col); if (BaseMath_WriteCallback(self) == -1) return -1; return 0; }
static void checker_board_color_fill(unsigned char *rect, float *rect_float, int width, int height) { int hue_step, y, x; float hsv[3], rgb[3]; hsv[1] = 1.0; hue_step = power_of_2_max_i(width / 8); if (hue_step < 8) hue_step = 8; for (y = 0; y < height; y++) { hsv[2] = 0.1 + (y * (0.4 / height)); /* use a number lower then 1.0 else its too bright */ for (x = 0; x < width; x++) { hsv[0] = (float)((double)(x / hue_step) * 1.0 / width * hue_step); hsv_to_rgb_v(hsv, rgb); if (rect) { rect[0] = (char)(rgb[0] * 255.0f); rect[1] = (char)(rgb[1] * 255.0f); rect[2] = (char)(rgb[2] * 255.0f); rect[3] = 255; rect += 4; } if (rect_float) { rect_float[0] = rgb[0]; rect_float[1] = rgb[1]; rect_float[2] = rgb[2]; rect_float[3] = 1.0f; rect_float += 4; } } } }
bool BKE_colorband_evaluate(const ColorBand *coba, float in, float out[4]) { const CBData *cbd1, *cbd2, *cbd0, *cbd3; float fac; int ipotype; int a; if (coba == NULL || coba->tot == 0) return false; cbd1 = coba->data; ipotype = (coba->color_mode == COLBAND_BLEND_RGB) ? coba->ipotype : COLBAND_INTERP_LINEAR; if (coba->tot == 1) { out[0] = cbd1->r; out[1] = cbd1->g; out[2] = cbd1->b; out[3] = cbd1->a; } else if ((in <= cbd1->pos) && ELEM(ipotype, COLBAND_INTERP_LINEAR, COLBAND_INTERP_EASE)) { out[0] = cbd1->r; out[1] = cbd1->g; out[2] = cbd1->b; out[3] = cbd1->a; } else { CBData left, right; /* we're looking for first pos > in */ for (a = 0; a < coba->tot; a++, cbd1++) { if (cbd1->pos > in) { break; } } if (a == coba->tot) { cbd2 = cbd1 - 1; right = *cbd2; right.pos = 1.0f; cbd1 = &right; } else if (a == 0) { left = *cbd1; left.pos = 0.0f; cbd2 = &left; } else { cbd2 = cbd1 - 1; } if ((in >= cbd1->pos) && ELEM(ipotype, COLBAND_INTERP_LINEAR, COLBAND_INTERP_EASE)) { out[0] = cbd1->r; out[1] = cbd1->g; out[2] = cbd1->b; out[3] = cbd1->a; } else { if (cbd2->pos != cbd1->pos) { fac = (in - cbd1->pos) / (cbd2->pos - cbd1->pos); } else { /* was setting to 0.0 in 2.56 & previous, but this * is incorrect for the last element, see [#26732] */ fac = (a != coba->tot) ? 0.0f : 1.0f; } if (ipotype == COLBAND_INTERP_CONSTANT) { /* constant */ out[0] = cbd2->r; out[1] = cbd2->g; out[2] = cbd2->b; out[3] = cbd2->a; } else if (ipotype >= COLBAND_INTERP_B_SPLINE) { /* ipo from right to left: 3 2 1 0 */ float t[4]; if (a >= coba->tot - 1) cbd0 = cbd1; else cbd0 = cbd1 + 1; if (a < 2) cbd3 = cbd2; else cbd3 = cbd2 - 1; CLAMP(fac, 0.0f, 1.0f); if (ipotype == COLBAND_INTERP_CARDINAL) { key_curve_position_weights(fac, t, KEY_CARDINAL); } else { key_curve_position_weights(fac, t, KEY_BSPLINE); } out[0] = t[3] * cbd3->r + t[2] * cbd2->r + t[1] * cbd1->r + t[0] * cbd0->r; out[1] = t[3] * cbd3->g + t[2] * cbd2->g + t[1] * cbd1->g + t[0] * cbd0->g; out[2] = t[3] * cbd3->b + t[2] * cbd2->b + t[1] * cbd1->b + t[0] * cbd0->b; out[3] = t[3] * cbd3->a + t[2] * cbd2->a + t[1] * cbd1->a + t[0] * cbd0->a; CLAMP(out[0], 0.0f, 1.0f); CLAMP(out[1], 0.0f, 1.0f); CLAMP(out[2], 0.0f, 1.0f); CLAMP(out[3], 0.0f, 1.0f); } else { float mfac; if (ipotype == COLBAND_INTERP_EASE) { mfac = fac * fac; fac = 3.0f * mfac - 2.0f * mfac * fac; } mfac = 1.0f - fac; if (UNLIKELY(coba->color_mode == COLBAND_BLEND_HSV)) { float col1[3], col2[3]; rgb_to_hsv_v(&cbd1->r, col1); rgb_to_hsv_v(&cbd2->r, col2); out[0] = colorband_hue_interp(coba->ipotype_hue, mfac, fac, col1[0], col2[0]); out[1] = mfac * col1[1] + fac * col2[1]; out[2] = mfac * col1[2] + fac * col2[2]; out[3] = mfac * cbd1->a + fac * cbd2->a; hsv_to_rgb_v(out, out); } else if (UNLIKELY(coba->color_mode == COLBAND_BLEND_HSL)) { float col1[3], col2[3]; rgb_to_hsl_v(&cbd1->r, col1); rgb_to_hsl_v(&cbd2->r, col2); out[0] = colorband_hue_interp(coba->ipotype_hue, mfac, fac, col1[0], col2[0]); out[1] = mfac * col1[1] + fac * col2[1]; out[2] = mfac * col1[2] + fac * col2[2]; out[3] = mfac * cbd1->a + fac * cbd2->a; hsl_to_rgb_v(out, out); } else { /* COLBAND_BLEND_RGB */ out[0] = mfac * cbd1->r + fac * cbd2->r; out[1] = mfac * cbd1->g + fac * cbd2->g; out[2] = mfac * cbd1->b + fac * cbd2->b; out[3] = mfac * cbd1->a + fac * cbd2->a; } } } } return true; /* OK */ }
void BKE_image_buf_fill_checker(unsigned char *rect, float *rect_float, int width, int height) { /* these two passes could be combined into one, but it's more readable and * easy to tweak like this, speed isn't really that much of an issue in this situation... */ int checkerwidth = 32, dark = 1; int x, y; unsigned char *rect_orig = rect; float *rect_float_orig = rect_float; float h = 0.0, hoffs = 0.0; float hsv[3] = {0.0f, 0.9f, 0.9f}; float rgb[3]; /* checkers */ for (y = 0; y < height; y++) { dark = powf(-1.0f, floorf(y / checkerwidth)); for (x = 0; x < width; x++) { if (x % checkerwidth == 0) dark = -dark; if (rect_float) { if (dark > 0) { rect_float[0] = rect_float[1] = rect_float[2] = 0.25f; rect_float[3] = 1.0f; } else { rect_float[0] = rect_float[1] = rect_float[2] = 0.58f; rect_float[3] = 1.0f; } rect_float += 4; } else { if (dark > 0) { rect[0] = rect[1] = rect[2] = 64; rect[3] = 255; } else { rect[0] = rect[1] = rect[2] = 150; rect[3] = 255; } rect += 4; } } } rect = rect_orig; rect_float = rect_float_orig; /* 2nd pass, colored + */ for (y = 0; y < height; y++) { hoffs = 0.125f * floorf(y / checkerwidth); for (x = 0; x < width; x++) { h = 0.125f * floorf(x / checkerwidth); if ((abs((x % checkerwidth) - (checkerwidth / 2)) < 4) && (abs((y % checkerwidth) - (checkerwidth / 2)) < 4)) { if ((abs((x % checkerwidth) - (checkerwidth / 2)) < 1) || (abs((y % checkerwidth) - (checkerwidth / 2)) < 1)) { hsv[0] = fmodf(fabsf(h - hoffs), 1.0f); hsv_to_rgb_v(hsv, rgb); if (rect) { rect[0] = (char)(rgb[0] * 255.0f); rect[1] = (char)(rgb[1] * 255.0f); rect[2] = (char)(rgb[2] * 255.0f); rect[3] = 255; } if (rect_float) { rect_float[0] = rgb[0]; rect_float[1] = rgb[1]; rect_float[2] = rgb[2]; rect_float[3] = 1.0f; } } } if (rect_float) rect_float += 4; if (rect) rect += 4; } } }