Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #14686

Re: Detect user graphics card?

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.teledata-fn.de!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail
Content-Type text/plain; charset="ISO-8859-1"
Message-ID <2493601.R5VuAm1OKa@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
Organization PointedEars Software (PES)
Date Tue, 26 Jun 2012 22:38:44 +0200
User-Agent KNode/4.4.11
Content-Transfer-Encoding 7Bit
X-Face %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
Face iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
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
MIME-Version 1.0
Lines 118
NNTP-Posting-Date 26 Jun 2012 22:38:44 CEST
NNTP-Posting-Host ee0d84e1.newsspool4.arcor-online.net
X-Trace DXC=HBNhMdjccQ2_A0jCfgHO6>4IUK<Cl32<14Fo<]lROoR18kF<OcfhCO;IHeiH@`A^P6DZm8W4\YJN<b@mF9jNikd5<g2D533;?;46h<]HKOoJQ8
X-Complaints-To usenet-abuse@arcor.de
Xref csiph.com comp.lang.javascript:14686

Followups directed to: comp.lang.javascript

Show key headers only | View raw


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


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