Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #14686
| Message-ID | <2493601.R5VuAm1OKa@PointedEars.de> (permalink) |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Organization | PointedEars Software (PES) |
| Date | 2012-06-26 22:38 +0200 |
| Subject | Re: Detect user graphics card? |
| Newsgroups | comp.lang.javascript |
| References | <e297bfd5-2f14-4f87-b6b4-92dc73c8b83d@r3g2000yqh.googlegroups.com> <js719u$7j2$1@dont-email.me> <4454122.7EdHYbU6jS@PointedEars.de> <jsc6bu$o4k$1@news.albasani.net> |
| Followup-To | comp.lang.javascript |
Followups directed to: comp.lang.javascript
Stefan Weiss wrote:
> On 2012-06-25 05:14, Thomas 'PointedEars' Lahn wrote:
>> JSX:color.js [1] defines jsx.dom.css.Color.prototype.getWebSafe() which
>> computes the nearest Web-safe color for a given color.
> ...
>> [1]
>>
<http://PointedEars.de/websvn/filedetails.php?repname=JSX&path=%2Ftrunk%2Fdom%2Fcss%2Fcolor.js>
>
> Interesting.
Thanks.
> As an aside,
How is that an aside? :)
> this function -
>
> function getNearestSafeValue (value)
> {
> if (value >= 0xFF)
> {
> return 0xFF;
> }
>
> if (value <= 0)
> {
> return 0;
> }
>
> for (var safeValues = [0, 0x33, 0x66, 0x99, 0xCC, 0xFF],
> i = safeValues.length - 1;
> i--;)
> {
> var item = safeValues[i];
>
> if (value >= item)
> {
> if (value == item)
> {
> return value;
> }
>
> var nextItem = safeValues[i + 1];
> if (value - item < nextItem - value)
> {
> return item;
> }
>
> return nextItem;
> }
> }
> return -1;
> }
>
> - could be simplified to:
>
> function getNearestSafeValue (n) {
> if (n >= 0xFF) {
> return 0xFF;
> }
> if (n <= 0) {
> return 0;
> }
> return Math.round(n / 0x33) * 0x33;
> }
Yes, indeed. I never considered multiples. I think it could be further
optimized as follows:
function getNearestSafeValue (value)
{
if (value >= 0xFF)
{
return 0xFF;
}
if (value <= 0)
{
return 0;
}
if (value % 0x33 == 0)
{
return value;
}
return Math.round(value / 0x33) * 0x33;
}
If there are no objections or further suggestions, I will use that from now
on.
> Or even in a single line (although less readable):
>
> function getNearestSafeValue (n) {
> return n >= 0xFF ? 0xFF : n <= 0 ? 0 : Math.round(n / 0x33) * 0x33;
> }
No, thanks :)
> A half-hearted benchmark indicates that the last version could be
> between 2x faster (Chrome, Opera) and 5x faster (Firefox) than the
> original.
AISB, I am not that interested in benchmark results as they are treacherous.
I am more interested in provably working, efficient algorithms. You have
provided one. Thank you.
PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Detect user graphics card? justaguy <lichunshen84@gmail.com> - 2012-06-24 04:31 -0700
Re: Detect user graphics card? Gregor Kofler <usenet@gregorkofler.com> - 2012-06-24 14:30 +0200
Re: Detect user graphics card? justaguy <lichunshen84@gmail.com> - 2012-06-24 07:43 -0700
Re: Detect user graphics card? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-06-25 05:14 +0200
Re: Detect user graphics card? justaguy <lichunshen84@gmail.com> - 2012-06-24 21:01 -0700
Re: Detect user graphics card? Stefan Weiss <krewecherl@gmail.com> - 2012-06-26 13:27 +0200
Re: Detect user graphics card? justaguy <lichunshen84@gmail.com> - 2012-06-26 12:54 -0700
Re: Detect user graphics card? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-06-26 22:38 +0200
Re: Detect user graphics card? Stefan Weiss <krewecherl@gmail.com> - 2012-06-27 01:05 +0200
Re: Detect user graphics card? Scott Sauyet <scott.sauyet@gmail.com> - 2012-06-27 07:07 -0700
Re: Detect user graphics card? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2012-06-29 16:41 -0700
Compute web-safe colors (was: Detect user graphics card?) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-06-30 10:40 +0200
Re: Detect user graphics card? Dr J R Stockton <reply1226@merlyn.demon.co.uk.not.invalid> - 2012-06-27 19:44 +0100
Re: Detect user graphics card? Arno Welzel <usenet@arnowelzel.de> - 2012-06-27 17:57 +0200
csiph-web