Exemplo n.º 1
0
void HexStringToBytes(const WCHAR* str, uint8* buf, int length)
{
	for (int i = 0; i < length; i++)
	{
		int l = Hex2Number(str[i*2+0]);
		int h = Hex2Number(str[i*2+1]);

		buf[i] = (l<<4) | h;
	}
}
Exemplo n.º 2
0
ErrorCode ParseRGBColor(/*[in]*/ const WCHAR* p, /*[out]*/ const WCHAR** pp, /*[out,retval]*/ Graphics::Color* pVal)
{
	ASSERT(pVal != NULL);
	ASSERT(pp != NULL);
	*pp = NULL;

	if (*p == '#')
	{
		p++;

		const WCHAR* s = p;
		while (*p && isxdigit(*p))
		{
			p++;
		}

		if (p-s == 3)
		{
			*pp = p;

			*pVal = Graphics::Color(255,
							(Hex2Number(s[0])<<4) | Hex2Number(s[0]),
							(Hex2Number(s[1])<<4) | Hex2Number(s[1]),
							(Hex2Number(s[2])<<4) | Hex2Number(s[2]));

			return 0;
		}
		else if (p-s == 6)
		{
			*pp = p;

			*pVal = Graphics::Color(255,
							(Hex2Number(s[0])<<4) | Hex2Number(s[1]),
							(Hex2Number(s[2])<<4) | Hex2Number(s[3]),
							(Hex2Number(s[4])<<4) | Hex2Number(s[5]));

			return 0;
		}
	}
	else if ((p[0] == 'r') && (p[1] == 'g') && (p[2] == 'b'))
	{
		p += 3;

		while (*p && *p == L' ') p++;	// Skip spaces

		if (*p++ != L'(') return -1;	// Error

		while (*p && *p == L' ') p++;	// Skip spaces
		double red = getfnumber(&p);
		if (p == NULL) return -1;
		if (*p == L'%')
		{
			p++;
			red = red * 255 / 100;
		}
		while (*p && *p == L' ') p++;	// Skip spaces
		if (*p++ != L',') return -1;

		while (*p && *p == L' ') p++;	// Skip spaces
		double green = getfnumber(&p);
		if (p == NULL) return -1;
		if (*p == L'%')
		{
			p++;
			green = green * 255 / 100;
		}

		while (*p && *p == L' ') p++;	// Skip spaces
		if (*p++ != L',') return -1;

		while (*p && *p == L' ') p++;	// Skip spaces
		double blue = getfnumber(&p);
		if (p == NULL) return -1;
		if (*p == L'%')
		{
			p++;
			blue = blue * 255 / 100;
		}

		while (*p && *p == L' ') p++;	// Skip spaces

		if (*p++ != L')') return -1;	// Error

		*pp = p;

		if (red < 0) red = 0;
		else if (red > 255) red = 255;

		if (green < 0) green = 0;
		else if (green > 255) green = 255;

		if (blue < 0) blue = 0;
		else if (blue > 255) blue = 255;

		*pVal = Graphics::Color(255, (uint8)red, (uint8)green, (uint8)blue);
		return 0;
	}
	else	// Named color
	{
		// TODO, smarter search given that array is sorted
		// TODO, make one array only
		int i = 0;

		ASSERT(0);
#if 0
		while (i < ColorNames_length)
		{
		//	int len = wcslen(ColorNames[i].name);

			if (ColorNames[i].get_Name() == p, len))
			{
				*pp = p + len;
				*pVal = ColorNames[i].clr | 0xff000000;
				return 0;
			}

			++i;
		}

		i = 0;
		while (SystemColors[i].name)
		{
			int len = wcslen(SystemColors[i].name);

			if (!wcsncmp(SystemColors[i].name, p, len))
			{
				*pp = p + len;
				*pVal = SystemColors[i].clr | 0xff000000;
				return 0;
			}

			i++;
		}
#endif
	}

	return -1;
}