예제 #1
0
파일: hsvpal.c 프로젝트: LiberatorUSA/GUCEF
static void
UpdateHSVFromRGBAv(AG_HSVPal *hsv)
{
	AG_Variable *bRGBAv;
	void *RGBAv;
	Uint8 r, g, b, a;
	float h, s, v;

	if ((bRGBAv = AG_GetVariable(hsv, "RGBAv", &RGBAv)) == NULL) {
		return;
	}
	switch (AG_VARIABLE_TYPE(bRGBAv)) {
	case AG_VARIABLE_FLOAT:
		r = (Uint8)(((float *)RGBAv)[0] * 255.0);
		g = (Uint8)(((float *)RGBAv)[1] * 255.0);
		b = (Uint8)(((float *)RGBAv)[2] * 255.0);
		a = (Uint8)(((float *)RGBAv)[3] * 255.0);
		break;
	case AG_VARIABLE_DOUBLE:
		r = (Uint8)(((double *)RGBAv)[0] * 255.0);
		g = (Uint8)(((double *)RGBAv)[1] * 255.0);
		b = (Uint8)(((double *)RGBAv)[2] * 255.0);
		a = (Uint8)(((double *)RGBAv)[3] * 255.0);
		break;
	case AG_VARIABLE_INT:
		r = (Uint8)(((int *)RGBAv)[0]);
		g = (Uint8)(((int *)RGBAv)[1]);
		b = (Uint8)(((int *)RGBAv)[2]);
		a = (Uint8)(((int *)RGBAv)[3]);
		break;
	case AG_VARIABLE_UINT8:
		r = ((Uint8 *)RGBAv)[0];
		g = ((Uint8 *)RGBAv)[1];
		b = ((Uint8 *)RGBAv)[2];
		a = ((Uint8 *)RGBAv)[3];
		break;
	default:
		r = 0;
		g = 0;
		b = 0;
		a = 0;
		break;
	}

	AG_RGB2HSV(r, g, b, &h, &s, &v);
	AG_SetFloat(hsv, "hue", h);
	AG_SetFloat(hsv, "saturation", s);
	AG_SetFloat(hsv, "value", v);
	SetAlpha8(hsv, a);
	AG_UnlockVariable(bRGBAv);
	hsv->flags |= AG_HSVPAL_DIRTY;
	AG_Redraw(hsv);
}
예제 #2
0
파일: hsvpal.c 프로젝트: LiberatorUSA/GUCEF
static void
UpdateHSVFromPixel(AG_HSVPal *hsv, Uint32 pixel)
{
	Uint8 r, g, b, a;
	float h, s, v;
	AG_Variable *bFormat;
	AG_PixelFormat **pFormat;
	
	bFormat = AG_GetVariable(hsv, "pixel-format", &pFormat);
	AG_GetPixelRGBA(pixel, *pFormat, &r,&g,&b,&a);
	AG_RGB2HSV(r, g, b, &h,&s,&v);
	AG_SetFloat(hsv, "hue", h);
	AG_SetFloat(hsv, "saturation", s);
	AG_SetFloat(hsv, "value", v);
	SetAlpha8(hsv, a);
	AG_UnlockVariable(bFormat);
	AG_Redraw(hsv);
}
예제 #3
0
파일: colors.c 프로젝트: varialus/agar
AG_Color
AG_ColorFromString(const char *s, const AG_Color *pColor)
{
	char buf[128];
	AG_Color cIn = (pColor != NULL) ? *pColor : AG_ColorRGB(0,0,0);
 	AG_Color cOut = cIn;
	double v[4];
	int isPct[4], isHSV = 0;
 	char *c, *pc;
	int i, argc;

	Strlcpy(buf, s, sizeof(buf));
	
	for (c = &buf[0]; *c != '\0' && isspace(*c); c++) {
		;;
	}
	switch (*c) {
	case 'r':		/* rgb(r,g,b[,a]) */
		break;
	case 'h':		/* hsv(h,s,v[,a]) */
		isHSV = 1;
		break;
	default:
		return (cOut);
	}
	for (; *c != '\0' && *c != '('; c++)
		;;
	if (*c == '\0' || c[1] == '\0') {
		goto out;
	}
	pc = &c[1];
	for (i = 0, argc = 0; i < 4; i++) {
		char *tok, *ep;
		if ((tok = AG_Strsep(&pc, ",")) == NULL) {
			break;
		}
		v[i] = strtod(tok, &ep);
		isPct[i] = (*ep == '%');
		argc++;
	}
	if (argc < 3) {
		goto out;
	}
	if (isHSV) {
		float hue, sat, val;

		AG_RGB2HSV(cIn.r, cIn.g, cIn.b, &hue, &sat, &val);
		hue = isPct[0] ? ColorPct(hue, v[0]) : v[0];
		sat = isPct[1] ? ColorPct(sat, v[1]) : v[1];
		val = isPct[2] ? ColorPct(val, v[2]) : v[2];
		AG_HSV2RGB(hue, sat, val, &cOut.r, &cOut.g, &cOut.b);
		if (argc == 4) {
			cOut.a = isPct[3] ? ColorPct(cIn.a, v[3]) : v[3];
		} else {
			cOut.a = cIn.a;
		}
	} else {
		cOut.r = isPct[0] ? (Uint8)ColorPct(cIn.r, v[0]) : (Uint8)v[0];
		cOut.g = isPct[1] ? (Uint8)ColorPct(cIn.g, v[1]) : (Uint8)v[1];
		cOut.b = isPct[2] ? (Uint8)ColorPct(cIn.b, v[2]) : (Uint8)v[2];
		if (argc == 4) {
			cOut.a = isPct[3] ? (Uint8)ColorPct(cIn.a, v[3]) : (Uint8)v[3];
		} else {
			cOut.a = cIn.a;
		}
	}
out:
	return (cOut);
}