Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="colorTest" :style="bgStyle">
  <h3>Input background color code:</h3>
  <input v-model="color" maxlength="6"></input> &nbsp;
  <input v-model="byHSP.v" type="checkbox" :title="byHSP.title">HSP</input>
  <span v-if="byHSP.v">
  <input v-model="byW3C.v" type="checkbox" :title="byW3C.title">W3C</input>
  </span>
  <br>
  <p>
    bg:
    <span class="box" :style="bgStyle">
      0x{{ bgColor }}
    </span>
    &nbsp; &nbsp; color:
    <span class="box" :style="fgStyle">
      0x{{ fgColor }}
    </span>
  </p>
</div>
              
            
!

CSS

              
                #colorTest {
  left: 0; top: 0;
  height: 100%; width: 100%;
  position: fixed;
  padding: 0pt 6pt;
}

input[type=checkbox] {
  cursor: pointer;
}

span.box {
  border: dotted 1px;
  padding: 3pt;
}
              
            
!

JS

              
                const defColorCode = 0xFFFF00
const flipColor = (color) => {
  let hex = parseHexColor(color)
  let hexStr = hexColor(hex)
  let hexGroup = splitBy(2, hexStr)
  let hv = hexValues(hex)
  // console.log(`hex: ${JSON.stringify(hexGroup, null, 2)}`)
  console.log(`val: ${JSON.stringify(hv, null, 2)}`)
  for (let i = 0; i < hv.length; i++) {
    let v = 0xFF - hv[i]
    if (Math.abs(hv[i] - 0x80) < 0x40) {
      v = (hv[i] + 0x80) % 0x100
      console.log(`RGB[${i}]: ${hv[i]} + 0x80 = ${hv[i] + 0x80} % 0x100 ==> ${v}`)
    }
    let x = v.toString(16).toUpperCase()
    hexGroup[i] = x.padStart(2, '0')
  }
  fc = hexGroup.join('')
  console.log(`flipColor: ${fc}`)
  return fc
}
const brightness = (color) => {
  // see http://www.nbdtech.com/Blog/archive/2008/04/27/Calculating-the-Perceived-Brightness-of-a-Color.aspx
  // and http://alienryderflex.com/hsp.html (by Darel Rex Finley)
  [r, g, b] = hexValues(parseHexColor(color))
  v = Math.sqrt(r * r * .241 + g * g * .691 + b * b * .068)
  console.log(`brightness: ${v}`)
  return v
}
const brightnessW3C = (color) => {
  // see https://24ways.org/2010/calculating-color-contrast
  [r, g, b] = hexValues(parseHexColor(color))
  v = (r * 299 + g * 587 + b * 114) / 1000
  // console.log(`brightness (W3C): ${v}`)
  return v
}
const calcBwHSP = (color) => {
  return brightness(color) < 130 ? 'FFFFFF' : '000000'
}
const calcBwW3C = (color) => {
  return brightnessW3C(color) <= 125 ? 'FFFFFF' : '000000'
}
const hexColor = (hex) => {
  let hexString = (hex%0x1000000).toString(16).toUpperCase()
  // return '0'.repeat(6-hexString.length) + hexString
  return hexString.padStart(6, '0')
}
const hexValues = (hex) => {
  let b = hex % 0x100
  let g = Math.ceil((hex - b) / 0x100) % 0x100
  let r = Math.ceil((hex - g) / 0x100) % 0x100
  return [r, g, b]
}
const parseHexColor = (color) => {
  let m = color.match(/^[0-9a-fA-F]+$/)
  let colorCode = m ? parseInt(`${m[0]}`, 16) : defColorCode
  return range(colorCode, 0, 0xFFFFFF)
}
const range = (v, min, max) => {
  if (v < min) return min
  if (v > max) return max
  return v
}
const splitBy = (pos, str) => {
  let index = str.length - pos
  if (index <= 0) return [str]
  let a = [ ...splitBy(2, str.slice(0, index)), str.slice(index) ]
  return a
}
const bwTitle = 'Use black/white text color'
const data = {
  byHSP: {v: false, title: `${bwTitle} by HSP Color Model`},
  byW3C: {v: false, title: `${bwTitle} by W3C algorithm`},
  color: hexColor(defColorCode)
}

new Vue({
  el: '#colorTest',
  data: () => data,
  computed: {
    bgColor: function () {
      return hexColor(parseHexColor(this.color))
    },
    bgStyle: function () {
      let style = {
        backgroundColor: `#${this.bgColor}`,
        color: `#${this.fgColor}`
      }
      // using style object
      // console.log(`body style: ${JSON.stringify(style, null, 2)}`)
      return style
    },
    fgColor: function () {
      if (this.byW3C.v && this.byHSP.v) {
        return calcBwW3C(this.bgColor)
      }
      return this.byHSP.v ? calcBwHSP(this.bgColor) : flipColor(this.bgColor)
      // let bgc = parseInt(this.bgColor, 16)
      // let hex = (bgc + 0x808080) % 0x1000000
      // return hexColor(hex)
    },
    fgStyle: function () {
      let bgStyle = `background-color: #${this.fgColor}`
      let fgStyle = `color: #${this.bgColor}`
      // using style string
      // console.log(`text style: ${bgStyle}; ${fgStyle}`)
      return `${bgStyle}; ${fgStyle}`
    }
  }
});

              
            
!
999px

Console