     $("#col-#{p}").change -> (scheme[sel] ?= {})[p] = @value; updateSampleStyle(); return
     $("#col-#{p}-cb").click ->
       $("#col-#{p}").toggle @checked
-      h = scheme[sel] ?= {}; if @checked then h[p] = $("#col-#{p}").val() else delete h[p]
+      h = scheme[sel] ?= {}; if @checked then h[p] = shrinkColour $("#col-#{p}").val() else delete h[p]
       updateSampleStyle(); return
     return
   qw('B I U').forEach (p) ->

builtInSchemes = [
  {
    name:'Default',frozen:1
    num:{fg:'#888888'},str:{fg:'#008888'},zld:{fg:'#000088'},var:{fg:'#888888'},quad:{fg:'#880088'}
    fn:{fg:'#000088'},op1:{fg:'#0000ff'},op2:{fg:'#0000ff'},ns:{fg:'#888888'},asgn:{fg:'#0000ff'}
    diam:{fg:'#0000ff'},par:{fg:'#0000ff'},brkt:{fg:'#0000ff'},semi:{fg:'#0000ff'},dfn:{fg:'#0000ff'}
    trad:{fg:'#888888'},kw:{fg:'#880000'},idm:{fg:'#0000ff'},com:{fg:'#008888'},err:{fg:'#ff0000'}
    lnum:{fg:'#000088'},mtch:{bg:'#ffff88'},srch:{bg:'#ff8800'},mod:{bg:'#eeeeee'},sel:{bg:'#d7d4f0'}
    sel:{bg:'#d9d9d9'}
  }
  {
    name:'Goya',frozen:1
    norm:{fg:'#97d07c',bg:'#000000'},cur:{lb:'#ff0000'},lnum:{fg:'#bb9944',bg:'#101810'}
    srch:{bg:'#b99260',fg:'#000000'},mod:{bg:'#111111'},sel0:{bg:'#112233'},sel:{bg:'#002244'}
    err:{fg:'#ff0000',bg:'#4d0f0f',B:1,U:1},kw:{fg:'#b0b01a'},num:{fg:'#ab8ebe'},op1:{fg:'#d4945f'},fn:{fg:'#00ff00'}
    op2:{fg:'#fddf59'},brkt:{fg:'#808080'},com:{fg:'#c0c0c0',I:1},semi:{fg:'#808080'},dfn:{fg:'#a277be'}
    str:{fg:'#d6b2ef'},zld:{fg:'#d79bff',B:1},lbl:{U:1,bg:'#39260a'},idm:{B:1},dfn3:{fg:'#cc739c'},dfn2:{fg:'#e3bd49'}
    dfn4:{fg:'#000000'}
  }
  {
    name:'Dürer',frozen:1
    num:{fg:'#808080'},str:{fg:'#808080'},zld:{fg:'#808080'},quad:{fg:'#880088'},ns:{fg:'#888888'},diam:{B:1}
    kw:{B:1},idm:{U:1,bg:'#f8f8f8'},com:{I:1},err:{fg:'#ffffff',bg:'#000000',B:1,I:1,U:1},mtch:{bg:'#cccccc'}
    srch:{bg:'#cccccc'},mod:{bg:'#eeeeee'},glb:{I:1}
  }
]


  join G.map (g) ->
    if h = scheme[g.t]
      g.c.split(',').map((x) -> rp + ' ' + x).join(',') + '{' +
        (h.fg && "color:#{expandColour h.fg};"             || '') +
        (h.bg && "background-color:#{expandColour h.bg};"  || '') +
        (h.B  && 'font-weight:bold;'                       || '') +
        (h.I  && 'font-style:italic;'                      || '') +
        (h.U  && 'text-decoration:underline;'              || '') +
        (h.lb && "border-left-color:#{expandColour h.lb};" || '') +
        '}'
    else
      ''

expandColour = (s) ->
  switch (s || '').length
    when 6 then '#'+s; when 3 then [r,g,b]=s;'#'+r+r+g+g+b+b; when 1 then '#'+s+s+s+s+s+s; else s

shrinkColour = (s) ->
  if !/^#.{6}$/.test s then s
  else [_,r,R,g,G,b,B]=s; if r==R==g==G==b==B then r else if r==R && g==G && b==B then r+g+b else s[1..]

$ updateStyle = -> # update global style from what's in localStorage; do it on "document ready"
  name = prefs.colourScheme()
