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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

              
                <section class="display">
  <div class="hex">
    <div class="colorpad"></div>
    <p class="colorCode">#808080</p>
  </div>
  <div class="rgb">
    <div class="colorpad"></div>
    <p class="colorCode">RGB(128,128,128)</p>
  </div>
  <div class="rgba">
    <div class="colorpad"></div>
    <p class="colorCode">RGBA(128,128,128,0.5)</p> 
  </div>
</section>

<section class="slider"> 
  <div class="red-group">
    <span>R</span>
    <input type="range" min="0" max="255" value="128" class="red">
    <span class="range">128</span>
  </div>

  <div class="green-group">
    <span>G</span>
    <input type="range" min="0" max="255" value="128" class="green">
    <span class="range">128</span>
  </div>

  <div class="blue-group">
    <span>B</span>
    <input type="range" min="0" max="255" value="128" class="blue">
    <span class="range">128</span>
  </div>
  
  <div class="alpha-group">
    <span>A</span>
    <input type="range" min="0" max="1" step="0.01" value="0.5" class="alpha">
    <span class="range">0.5</span>
  </div>

</section>


              
            
!

CSS

              
                * {
  font-size: 13px;
/*   outline: 1px solid green; */
  margin: 0;
  padding: 0;
}

body {
  background-color: #fff;
/*   background-color: #9D4B80; */
  margin: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.display {
  display: flex;
  justify-content: space-between;
  margin-bottom: 30px;
  
}

.display div {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.display p {
  width: 160px;
  text-align: center;
  background-color: rgb(78,199,207);
  padding: 8px 14px;
  border-radius: 20px;
  box-shadow: 0px 2px 5px rgb(0,0,80);
  transition: transform 0.3s; 
}

.display p:hover {
  cursor: pointer;
  transform: scale(1.02);
}

.colorpad {
  width: 150px;
  height: 150px;
  border: 1px solid #c0c0c0;
  border-radius: 50%;
  background-color: rgb(128,128,128);
}

.rgba .colorpad {
  background-color: rgb(128,128,128, 0.5);
}


section {
  width: 600px;
}

div {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-bottom: 15px;
  
}


div > span {
  width: 40px;
  padding: 5px 0;
  text-align: center;
  border-radius: 15px;
}

.red-group span {
  background-color: rgba(255, 0, 0, 0.9);
  box-shadow: 1px 1px 5px rgb(0,0,0);
  color: snow;
}

.green-group span {
  background-color: rgba(0, 255, 0, 0.9);
  box-shadow: 1px 1px 5px rgb(0,0,0);
}

.blue-group span {
  background-color: rgba(0, 0, 255, 0.9);
  box-shadow: 1px 1px 5px rgb(0,0,0);
  color: snow;
}

.alpha-group span {
  background-color: rgba(197, 209, 227, 1);
  box-shadow: 1px 1px 5px rgb(0,0,0);
}


input:hover {
  cursor: pointer;
}

.range {
  display: flex;
  justify-content: center;
  align-items: center;
}


/* .hex p {
  width: 150px;
  text-align: center;
  letter-spacing: 2px;
  background-color: rgb(78,199,207);
  border: 1px solid rgb(3,3,3);
  border-radius: 20px;
  box-shadow: 1px 1px 5px rgb(0,0,0)
} */


              
            
!

JS

              
                const get = (selector) => document.querySelector(selector);
const getAll = (selector) => document.querySelectorAll(selector);

const rangeSliders  = getAll('input');
const red = get('.red');
const green = get('.green')
const blue = get('.blue')
const alpha = get('.alpha')


rangeSliders.forEach(rangeSlider => {
  //監聽並改變RGB個別代碼
  rangeSlider.addEventListener('mousemove', displayDigits);
  //監聽並改變所有色碼表示法
  rangeSlider.addEventListener('mousemove', displayColorCode);
  //監聽並改變背景顏色
  rangeSlider.addEventListener('mousemove', changeColor);
})

// 數值轉換 
// https://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb

function rgbToHex(R,G,B) {
  const hexDigit = [R, G, B].map( item => {
    return Number(item).toString(16).padStart(2, '0')
  }).join('')
  
  return '#'+ hexDigit;
}

// 顯示個別數值
function displayDigits(e) {
  const currentValue = e.target.value;
  const alphaValue = currentValue;
  e.target.nextElementSibling.innerHTML = currentValue;
}


//顯示顏色的不同表示法
function displayColorCode() {
  const hexValue = (rgbToHex(red.value, green.value, blue.value)).toUpperCase()
  
  const hexIndicator = get('.hex').children[1]
  const rgbIndicator = get('.rgb').children[1]
  const rgbaIndicator = get('.rgba').children[1]
  
  hexIndicator.innerHTML = hexValue;
  rgbIndicator.innerHTML = `RGB(${red.value},${green.value},${blue.value})`
  rgbaIndicator.innerHTML = `RGBA(${red.value},${green.value},${blue.value},${alpha.value})`
}


//改變顏色
function changeColor() {
  const hexColorPad = get('.hex').children[0]
  const rgbColorPad = get('.rgb').children[0]
  const rgbaColorPad = get('.rgba').children[0]
   
  hexColorPad.style.backgroundColor = `rgb(${red.value},${green.value},${blue.value})`;
  
  rgbColorPad.style.backgroundColor = `rgb(${red.value},${green.value},${blue.value})`;
  
  rgbaColorPad.style.backgroundColor = `rgba(${red.value},${green.value},${blue.value}, ${alpha.value})`;
}


//做複製的功能
const buttons = getAll('.colorCode');

buttons.forEach(button => {
  button.addEventListener('click', copy);
  button.addEventListener('click', changeDisplay);
});


function copy() {
  const range = document.createRange();
  range.selectNode(this);
  // range.selectNode(button)
  const selection = window.getSelection();
  
  selection.removeAllRanges();
  selection.addRange(range);
  
  
  document.execCommand('copy');
  selection.removeAllRanges();
}

function changeDisplay(e){
  e.target.style.backgroundColor = `rgb(247,157,128)`;
  e.target.innerHTML = `複製好色碼囉~`
  
  reset(e)
}

function reset(e){
  setTimeout(()=> {
    e.target.style.backgroundColor = `rgb(78,199,207)`;
    displayColorCode();
  }, 2000)
}
















              
            
!
999px

Console