-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #137 from woowa-techcamp-2021/develop
v0.5 release
- Loading branch information
Showing
18 changed files
with
450 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
const colors = { | ||
primary: '#2AC1BC' | ||
primary: '#2AC1BC', | ||
offWhite: '#FCFCFC', | ||
titleActive: '#1E2222' | ||
}; | ||
|
||
export default colors; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
.color-picker { | ||
width: 100%; | ||
display: flex; | ||
/* flex-direction: column; */ | ||
align-items: center; | ||
justify-content: space-between; | ||
margin: 10px 0; | ||
} | ||
|
||
.color-picker__palette { | ||
display: flex; | ||
align-items: center; | ||
justify-content: space-between; | ||
width: 45%; | ||
} | ||
|
||
.color-picker__color { | ||
width: 25px; | ||
height: 25px; | ||
border-radius: 8px; | ||
cursor: pointer; | ||
} | ||
|
||
.color-picker__input-wrapper { | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
} | ||
|
||
.color-picker__color-hex { | ||
background-color: var(--background); | ||
width: 120px; | ||
height: 30px; | ||
border: 1px solid var(--line); | ||
border-radius: 8px; | ||
outline: none; | ||
} | ||
|
||
.color-picker__current { | ||
width: 25px; | ||
height: 25px; | ||
border-radius: 8px; | ||
} | ||
|
||
.color-picker__refresh { | ||
width: 25px; | ||
height: 25px; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
margin-left: 10px; | ||
font-size: var(--bold-medium); | ||
font-weight: bold; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import colors from '../../assets/styles/colors'; | ||
import UIElement from '../../core/ui-element'; | ||
import { generateRandomColor } from '../../utils/color'; | ||
|
||
import './index.css'; | ||
|
||
const COLOR_COUNT = 5; | ||
|
||
class ColorPickerUIElement extends UIElement { | ||
private colors: string[] = []; | ||
private $palette?: HTMLElement; | ||
private $colorInput?: HTMLInputElement; | ||
private $refreshButton?: HTMLElement; | ||
|
||
constructor ($target: HTMLElement) { | ||
super($target, { | ||
className: 'color-picker' | ||
}); | ||
} | ||
|
||
get value (): string | undefined { | ||
return this.$colorInput?.value; | ||
} | ||
|
||
clear (): void { | ||
if (this.$colorInput !== undefined) { | ||
this.$colorInput.value = ''; | ||
} | ||
} | ||
|
||
private assignColors () { | ||
this.colors = []; | ||
|
||
for (let i = 0; i < COLOR_COUNT; i += 1) { | ||
this.colors.push(generateRandomColor()); | ||
} | ||
} | ||
|
||
private renderPalette () { | ||
if (this.$palette === undefined) { | ||
return; | ||
} | ||
|
||
this.$palette.innerHTML = this.colors.map((color) => { | ||
return ` | ||
<div data-color=${color} class="color-picker__color" style="background-color: ${color}"></div> | ||
`; | ||
}).join(''); | ||
} | ||
|
||
protected render (): void { | ||
this.assignColors(); | ||
} | ||
|
||
private getFontColorByBackground (color: string): string | undefined { | ||
const RGB_SUM_STD = 600; | ||
|
||
if (!color.match(/^#(?:[0-9a-f]{3}){1,2}$/i)) { | ||
return; | ||
} | ||
|
||
const r = parseInt(color.substring(1, 3), 16); | ||
const g = parseInt(color.substring(3, 5), 16); | ||
const b = parseInt(color.substring(5, 7), 16); | ||
|
||
if (r + g + b < RGB_SUM_STD) { | ||
return colors.offWhite; | ||
} | ||
|
||
return colors.titleActive; | ||
} | ||
|
||
private setCurrentColor (color: string) { | ||
if (this.$colorInput !== undefined && this.$refreshButton) { | ||
this.$colorInput.value = color; | ||
this.$refreshButton.style.backgroundColor = color; | ||
const fontColor = this.getFontColorByBackground(color); | ||
fontColor && this.$refreshButton.style.setProperty('color', fontColor); | ||
} | ||
} | ||
|
||
private onPaletteClicked (e: Event) { | ||
const target = e.target as HTMLElement; | ||
const { color } = target.dataset; | ||
if (color === undefined) { | ||
return; | ||
} | ||
|
||
this.setCurrentColor(color); | ||
} | ||
|
||
private onColorInput (e: Event) { | ||
const { value } = e.target as HTMLInputElement; | ||
this.setCurrentColor(value); | ||
} | ||
|
||
private onRefreshClicked () { | ||
this.assignColors(); | ||
this.renderPalette(); | ||
} | ||
|
||
protected addListener (): void { | ||
this.$palette?.addEventListener('click', this.onPaletteClicked.bind(this)); | ||
this.$colorInput?.addEventListener('input', this.onColorInput.bind(this)); | ||
this.$refreshButton?.addEventListener('click', this.onRefreshClicked.bind(this)); | ||
} | ||
|
||
protected mount (): void { | ||
this.$palette = document.createElement('div'); | ||
this.$palette.className = 'color-picker__palette'; | ||
this.$element.appendChild(this.$palette); | ||
this.renderPalette(); | ||
|
||
const $colorInputWrapper = document.createElement('div'); | ||
$colorInputWrapper.className = 'color-picker__input-wrapper'; | ||
this.$element.appendChild($colorInputWrapper); | ||
|
||
this.$colorInput = document.createElement('input'); | ||
this.$colorInput.className = 'color-picker__color-hex'; | ||
$colorInputWrapper.appendChild(this.$colorInput); | ||
|
||
this.$refreshButton = document.createElement('div'); | ||
this.$refreshButton.className = 'color-picker__refresh color-picker__color'; | ||
this.$refreshButton.innerHTML = 'RE'; | ||
$colorInputWrapper.appendChild(this.$refreshButton); | ||
} | ||
} | ||
|
||
export default ColorPickerUIElement; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const generateRandomColor = (): string => { | ||
return `#${(0x1000000 + Math.random() * 0xffffff).toString(16).substr(1, 6)}`; | ||
}; |
Oops, something went wrong.