{"id":261,"date":"2010-05-20T21:59:36","date_gmt":"2010-05-21T04:59:36","guid":{"rendered":"http:\/\/www.jusquici.org\/blog\/?p=261"},"modified":"2010-05-24T10:16:57","modified_gmt":"2010-05-24T17:16:57","slug":"towards-a-good-nights-rest","status":"publish","type":"post","link":"https:\/\/www.jusquici.org\/blog\/?p=261","title":{"rendered":"towards a good night&#8217;s rest"},"content":{"rendered":"<p>Throughout college, suffice to say, I didn&#8217;t sleep much.  If you know me well, you probably know the myriad reasons for this.<\/p>\n<p>Nowadays, artificial lighting can cause the Dim-Light Melatonin Offset to be pushed back.  Anyone who has lived on a farm or near a construction site knows that until recently, the light of the sun was the only light by which we could work, and our bodies adjusted accordingly.<\/p>\n<p>It&#8217;s not much of a surprise, then, that staring into the bright light known as a &#8220;monitor&#8221; can impact some people&#8217;s sleep schedule vastly.  It&#8217;s supplanted by the fact that for many people, night time frees them from the distractions of the day &#8211; people and the world outside in general.<\/p>\n<h2>amber on mac<\/h2>\n<p>I was fortunate enough to find an application for my Mac called <a href=\"http:\/\/docs.blacktree.com\/nocturne\/nocturne\">Nocturne<\/a> (from the makers of Quicksilver, in fact).  It inverts the colors and can even tint a monochromed version of the inverted image &#8211; think green or amber terminal.  An amber tint removes the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Melatonin#Light_dependence\">blue light<\/a> that is known to principally inhibit the production of melatonin, setting your monitor to this amber-on-black setting in the evening may help things out.<\/p>\n<h2>amber on linux<\/h2>\n<p>Compiz provides many of the shiny effects on Ubuntu.  The CompizConfig Settings Manager (not installed by default &#8211; check apt) has a color filter plugin available primarily for those with colorblindness.  However, negative and negative-green options are provided for low-light computing.  Green is a nice choice, but amber will give us the best results.  Unfortunately, there&#8217;s no easy way to set the tint from CompizConfig.<\/p>\n<h3>doing things the linux way<\/h3>\n<h4>or, an adventure in OpenGL ARB fragment programs.<\/h4>\n<p>The filter configurations are hidden away in the<\/p>\n<pre>\/usr\/share\/compiz\/filters<\/pre>\n<p>folder.  let&#8217;s check out the source for negative-green:<\/p>\n<blockquote><p><code>!!ARBfp1.0<br \/>\nTEMP temp, neg;<br \/>\nTEX temp, fragment.texcoord[0], texture[0], RECT;<br \/>\nRCP neg.a, temp.a;<br \/>\nMAD temp.rgb, -neg.a, temp, 1.0;<br \/>\nMUL temp.rgb, temp.a, temp;<br \/>\nMUL temp, fragment.color, temp;<br \/>\nSUB temp.rb, temp, temp;<br \/>\nMOV result.color, temp;<br \/>\nEND<\/code><\/p><\/blockquote>\n<p>This is no <a href=\"http:\/\/en.wikipedia.org\/wiki\/GLSL\">GLSL<\/a>, folks.  This is the real deal: <a href=\"http:\/\/en.wikipedia.org\/wiki\/ARB_(GPU_assembly_language)\">ARB OpenGL Assembly Language<\/a> &#8211; specifically, a fragment program.  It probably looks somewhat familiar if you&#8217;ve seen x86 or RISC assembly before.<\/p>\n<p>I won&#8217;t say the approach used to invert the pixels is trivial, but we can eventually spot where the green tint is being applied:<\/p>\n<blockquote><p><code>SUB temp.rb, temp, temp;<\/code><\/p><\/blockquote>\n<p>This line takes the vector temp, subtracts temp (yielding the zero vector), and stores it in the .rb component of temp.  Because temp is a color vector, the r and b stand for red and blue, leaving only the green component and alpha component intact.<\/p>\n<p>Converting this to be a pure-red filter is simple enough &#8211; we&#8217;ll just subtract green instead of red and be set:<\/p>\n<blockquote><p><code>SUB temp.gb, temp, temp;<\/code><\/p><\/blockquote>\n<p>This makes a very nice red tinted inverted filter.<\/p>\n<h3>inevitable last 2% difficulty<\/h3>\n<p>Next, of course, I wanted it a bit dimmer &#8211; 100% red is still fairly blinding in the dark.  Easy, except for the catch with open source:<\/p>\n<p>Modifying someone else&#8217;s code is one thing.  Writing your own is different.<\/p>\n<p>I honestly had no idea what I was doing in ARB, but RenderGuild kindly provides an <a href=\"http:\/\/www.renderguild.com\/gpuguide.pdf\">ARB assembly quick reference<\/a>.  Multiplying the .r component of my color seemed like the right thing to do, and to an extent, I was right.  I also now believe that the alpha component (.a) has to be dimmed.  Based on those two needs, I came up with these two instructions:<\/p>\n<blockquote><p><code>MUL tempColor.a, 0.6, tempColor.a;<br \/>\nMUL tempColor.r, 0.6, tempColor.r;<\/code><\/p><\/blockquote>\n<p>Perhaps there needs to only be one or the other, and perhaps they could be combined into a single instruction.  It does, however, work to my liking.<\/p>\n<h3>notes on writing shaders<\/h3>\n<p>Mac OS X&#8217;s <a href=\"http:\/\/developer.apple.com\/graphicsimaging\/opengl\/shader_image.html\">OpenGL Shader Builder<\/a> was invaluable to checking the compilation of my sloppy coding and, more importantly, hinting at why it was wrong.<\/p>\n<h3>notes on Compiz filter switching<\/h3>\n<p>While I was working on the shader, I had some strange things such as near-all-red and near-all-black displays happen, with fading at the corners.  I&#8217;m unsure why this happens, and may be a bug.  Cycling back through again usually puts you back where you expect to be.<\/p>\n<h1>the grand results<\/h1>\n<p>Here&#8217;s the source to my fragment shader.  It can be downloaded <a href=\"\/random\/negative-red-dim.fp\">here<\/a>.<\/p>\n<blockquote><p><code>!!ARBfp1.0<br \/>\nTEMP tempColor, negColor;<br \/>\nTEX tempColor, fragment.texcoord[0], texture[0], RECT;<br \/>\nRCP negColor.a, tempColor.a;<br \/>\nMAD tempColor.rgb, -negColor.a, tempColor, 1.0;<br \/>\nMUL tempColor.rgb, tempColor.a, tempColor;<br \/>\nMUL tempColor, fragment.color, tempColor;<br \/>\nSUB tempColor.gb, tempColor, tempColor;<br \/>\n# here is where we will dim.<br \/>\nMUL tempColor.a, .6, tempColor.a;<br \/>\nMUL tempColor.r, 0.6, tempColor.r;<br \/>\nMOV result.color, tempColor;<br \/>\nEND<\/code><\/p><\/blockquote>\n<h2>endnotes<\/h2>\n<p>I experimented with this kind of filtering during college starting at about 22h00 with a regular bedtime between 00h00 and 01h00.  Now that I need to be sleeping sooner, I&#8217;ll likely push that back to&#8230; whenever sunset is.<\/p>\n<p>This isn&#8217;t the solve-all for my sleep problems.  Waking is still difficult to predict &#8211; blackout drapes are necessary for me to sleep, and therefore the natural light cues are absent for waking.  With an alarm, I wake up to a dark vacuous room, which isn&#8217;t hugely motivating.  I&#8217;ll be experimenting with an iPhone app (Climate) and perhaps building a gradual-wake LED light with my Arduino.<\/p>\n<p>However, it has helped, and hopefully it can help someone else too.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Throughout college, suffice to say, I didn&#8217;t sleep much. If you know me well, you probably know the myriad reasons for this. Nowadays, artificial lighting can cause the Dim-Light Melatonin Offset to be pushed back. Anyone who has lived on a farm or near a construction site knows that until recently, the light of the &hellip; <a href=\"https:\/\/www.jusquici.org\/blog\/?p=261\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">towards a good night&#8217;s rest<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_markdown_editor_remember":false,"footnotes":""},"categories":[46,36,35,15],"tags":[],"_links":{"self":[{"href":"https:\/\/www.jusquici.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/261"}],"collection":[{"href":"https:\/\/www.jusquici.org\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jusquici.org\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jusquici.org\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jusquici.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=261"}],"version-history":[{"count":11,"href":"https:\/\/www.jusquici.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/261\/revisions"}],"predecessor-version":[{"id":277,"href":"https:\/\/www.jusquici.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/261\/revisions\/277"}],"wp:attachment":[{"href":"https:\/\/www.jusquici.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jusquici.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jusquici.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}