Skip to content

GitHub code colors

Rob Garrison edited this page Sep 22, 2017 · 54 revisions

A userscript that adds a color swatch next to the code color definition

  • It works in comment previews and code pages.
  • Supports hex, rgb, rgba, hsl, hsla and all named colors.
  • Additional support for colors set inside strings (e.g. javascript or canvas).
  • It does not interfere if you copy & paste the code.
  • Click this link to install from GitHub; or, install from GreasyFork or OpenUserJS.

Screenshot

github-code-color

Examples

Test these examples once the userscript is installed

CSS

.style {
  background: #123456;
  color: rgb( 255,128,64);
  border-left: 1px hsla(65, 100%, 50%, 1) solid;
  border-right-color: maroon;
  box-shadow: 3px 3px hsl( 128, 30%, 50% );
  text-shadow: 2px 5px rgba(255, 0,  183,0.5);
}

Javascript (Canvas)

// code slightly modified from the source:
// https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Applying_styles_and_colors#A_globalAlpha_example
function draw() {
  var ctx = document.getElementById('canvas').getContext('2d');
  // draw background
  ctx.fillStyle = 'gold';
  ctx.fillRect(0,0,75,75);
  ctx.fillStyle = '#6C0';
  ctx.fillRect(75,0,75,75);
  ctx.fillStyle = '#09F';
  ctx.fillRect(0,75,75,75);
  ctx.fillStyle = 'hsl(12, 100%, 50%)';
  ctx.fillRect(75,75,75,75);
  ctx.fillStyle = 'rgba(255, 255, 255, 1)';
  // set transparency value
  ctx.globalAlpha = 0.2;

  // Draw semi transparent circles
  for (i=0;i<7;i++){
    ctx.beginPath();
    ctx.arc(75,75,10+10*i,0,Math.PI*2,true);
    ctx.fill();
  }
}
draw();

Change Log

Version 1.1.9 (9/22/2017)

  • Prevent JS error on invalid css colors.

Version 1.1.8 (5/16/2017)

  • Change license to MIT.

Version 1.1.7 (4/21/2017)

  • Update mutation url.

Version 1.1.4 - 1.1.6 (4/13/2017)

  • Switch to using mutations.js.

Version 1.1.3 (4/5/2017)

Version 1.1.2 (3/29/2017)

  • Fix misspelled color name.

Version 1.1.1 (3/25/2017)

  • Use pjax & preview events.

Version 1.1.0 (9/12/2016)

  • Add "grey" color names.
  • Beautified.

Version 1.0.2 (8/20/2016)

  • General cleanup & update to es6.

Version 1.0.1 (3/25/2016)

  • Support unix style hex colors (e.g. 0x9988aa).
  • Support colors inside strings.
  • Cache regex in loop.

Version 1.0.0 (3/24/2016)

Version 0.1.2 (3/20/2016)

  • Remove flag preventing updates after ajax load.
  • Include named colors.

Version 0.1.1 (3/20/2016)

  • Add userscript meta data

Version 0.1.0 (3/20/2016)

  • Initial commit
Clone this wiki locally