Skip to content

Commit 6a0b5c8

Browse files
committed
use integer math for intensity, improve accuracy
1 parent 319cb81 commit 6a0b5c8

2 files changed

Lines changed: 8 additions & 8 deletions

File tree

src/tinylor.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ int lor_set_effect(lor_req_s* req, const lor_effect e,
3434
break;
3535
}
3636
req->effect = e;
37-
req->args = args != NULL ? *args : (lor_effect_args_u){0};
37+
req->args = args != NULL ? *args : (lor_effect_args_u) {0};
3838
return 0;
3939
}
4040

@@ -157,7 +157,7 @@ size_t lor_write(unsigned char* b, const size_t bs, const lor_req_s* r,
157157
}
158158

159159
lor_intensity lor_get_intensity(const unsigned char b) {
160-
// scale b from (0,255) to (240,1) which is the LOR intensity range
161-
static const lor_intensity ceiling = 240;
162-
return ceiling - (lor_intensity) ((1.0f - ((float) b / 255.0f)) * 239);
160+
const int intensity = (b * 100 + 127) / 255; // scale 0-255 to 0-100
161+
if (intensity >= 100) return 1; // clamp if above max intensity
162+
return (lor_intensity) (228 - intensity * 2);// inverted linear scale
163163
}

tinylor.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ int lor_set_effect(lor_req_s* req, const lor_effect e,
273273
break;
274274
}
275275
req->effect = e;
276-
req->args = args != NULL ? *args : (lor_effect_args_u){0};
276+
req->args = args != NULL ? *args : (lor_effect_args_u) {0};
277277
return 0;
278278
}
279279

@@ -396,9 +396,9 @@ size_t lor_write(unsigned char* b, const size_t bs, const lor_req_s* r,
396396
}
397397

398398
lor_intensity lor_get_intensity(const unsigned char b) {
399-
// scale b from (0,255) to (240,1) which is the LOR intensity range
400-
static const lor_intensity ceiling = 240;
401-
return ceiling - (lor_intensity) ((1.0f - ((float) b / 255.0f)) * 239);
399+
const int intensity = (b * 100 + 127) / 255; // scale 0-255 to 0-100
400+
if (intensity >= 100) return 1; // clamp if above max intensity
401+
return (lor_intensity) (228 - intensity * 2);// inverted linear scale
402402
}
403403

404404
#endif// TINYLOR_IMPL_ONCE

0 commit comments

Comments
 (0)