simplify PackedCol_Unhex

This commit is contained in:
UnknownShadow200 2019-07-22 18:01:46 +10:00
parent a32ffdab30
commit 2dc6a143f6
4 changed files with 22 additions and 22 deletions

View file

@ -85,11 +85,7 @@ static void Json_ConsumeString(struct JsonContext* ctx, String* str) {
/* form of \uYYYY */
if (c != 'u' || ctx->left < 4) break;
if (!PackedCol_Unhex(ctx->cur[0], &h[0])) break;
if (!PackedCol_Unhex(ctx->cur[1], &h[1])) break;
if (!PackedCol_Unhex(ctx->cur[2], &h[2])) break;
if (!PackedCol_Unhex(ctx->cur[3], &h[3])) break;
if (!PackedCol_Unhex(ctx->cur, h, 4)) break;
codepoint = (h[0] << 12) | (h[1] << 8) | (h[2] << 4) | h[3];
/* don't want control characters in names/software */

View file

@ -21,16 +21,22 @@ void PackedCol_GetShaded(PackedCol normal, PackedCol* xSide, PackedCol* zSide, P
*yMin = PackedCol_Scale(normal, PACKEDCOL_SHADE_YMIN);
}
bool PackedCol_Unhex(char hex, int* value) {
*value = 0;
static int PackedCol_DeHex(char hex) {
if (hex >= '0' && hex <= '9') {
*value = (hex - '0');
return (hex - '0');
} else if (hex >= 'a' && hex <= 'f') {
*value = (hex - 'a') + 10;
return (hex - 'a') + 10;
} else if (hex >= 'A' && hex <= 'F') {
*value = (hex - 'A') + 10;
} else {
return false;
return (hex - 'A') + 10;
}
return -1;
}
bool PackedCol_Unhex(const char* src, int* dst, int count) {
int i;
for (i = 0; i < count; i++) {
dst[i] = PackedCol_DeHex(src[i]);
if (dst[i] == -1) return false;
}
return true;
}
@ -42,7 +48,7 @@ void PackedCol_ToHex(String* str, PackedCol value) {
}
bool PackedCol_TryParseHex(const String* str, PackedCol* value) {
int rH, rL, gH, gL, bH, bL;
int bits[6];
char* buffer;
buffer = str->buffer;
@ -51,15 +57,13 @@ bool PackedCol_TryParseHex(const String* str, PackedCol* value) {
/* accept XXYYZZ or #XXYYZZ forms */
if (str->length < 6) return false;
if (str->length > 6 && (str->buffer[0] != '#' || str->length > 7)) return false;
if (buffer[0] == '#') buffer++;
if (!PackedCol_Unhex(buffer, bits, 6)) return false;
if (!PackedCol_Unhex(buffer[0], &rH) || !PackedCol_Unhex(buffer[1], &rL)) return false;
if (!PackedCol_Unhex(buffer[2], &gH) || !PackedCol_Unhex(buffer[3], &gL)) return false;
if (!PackedCol_Unhex(buffer[4], &bH) || !PackedCol_Unhex(buffer[5], &bL)) return false;
value->R = (uint8_t)((rH << 4) | rL);
value->G = (uint8_t)((gH << 4) | gL);
value->B = (uint8_t)((bH << 4) | bL);
value->R = (uint8_t)((bits[0] << 4) | bits[1]);
value->G = (uint8_t)((bits[2] << 4) | bits[3]);
value->B = (uint8_t)((bits[4] << 4) | bits[5]);
value->A = 255;
return true;
}

View file

@ -30,7 +30,7 @@ CC_API PackedCol PackedCol_Scale(PackedCol value, float t);
/* Linearly interpolates RGB components of the two given colours. */
CC_API PackedCol PackedCol_Lerp(PackedCol a, PackedCol b, float t);
CC_NOINLINE bool PackedCol_Unhex(char hex, int* value);
CC_NOINLINE bool PackedCol_Unhex(const char* src, int* dst, int count);
CC_NOINLINE void PackedCol_ToHex(String* str, PackedCol value);
CC_NOINLINE bool PackedCol_TryParseHex(const String* str, PackedCol* value);

View file

@ -2135,7 +2135,7 @@ int Platform_GetCommandLineArgs(int argc, STRING_REF char** argv, String* args)
void Platform_SetDefaultCurrentDirectory(void) {
struct android_app* app = (struct android_app*)App_Ptr;
const char* storageDir = app->activity->externalDataPath;
const char* storageDir = app->activity->externalDataPath;
ReturnCode res = chdir(storageDir) == -1 ? errno : 0;
if (res) Logger_Warn(res, "setting current directory");