JavaScript實(shí)現(xiàn)簡(jiǎn)單獲取本地圖片主色調(diào)
一、實(shí)現(xiàn)效果
鮮花
大海
森林
二、實(shí)現(xiàn)
1、實(shí)現(xiàn)思路
其實(shí)思路很簡(jiǎn)單,就是將一張大圖先縮小為一張小圖,再遍歷里面的像素,找到出現(xiàn)次數(shù)相對(duì)較高的一個(gè);當(dāng)然,先說(shuō)明一下,這個(gè)也只能實(shí)現(xiàn)一個(gè)提取近似的值或者跟“人的意識(shí)”相反的值,因此,最終結(jié)果的“滿意程度”可能不是很好。
2、實(shí)現(xiàn)代碼
創(chuàng)建一個(gè) ThemeColor 操作對(duì)象,通過(guò)回調(diào)返回縮略圖、主色調(diào) ,可進(jìn)行相關(guān)的其他操作
//本地圖片資源 let url = 'tree.webp' document.getElementById('originalImage').src = url let themeColor = new ThemeColor(url,(shrinkUrl,color)=>{ //縮略圖 let img = document.getElementById('showImage') if(img){ img.setAttribute('src',shrinkUrl) } //主色 document.getElementById('showDiv').style.backgroundColor = color })
ThemeColor.js
class ThemeColor{ // 原圖資源 imgUrl = '' // 像素集合 originalPiexls = null // 縮略圖 shrinkUrl = '' // 主色 themeColor = 'white' // 回調(diào) themeColorCallBack = null // 提取像素出現(xiàn)最大次數(shù)操作對(duì)象 colorCountedSet = new ColorCountedSet() constructor(imgUrl,callBack){ this.imgUrl = imgUrl this.themeColorCallBack = callBack this.startScreeningThemeColor() } // 開(kāi)始解析主色 async startScreeningThemeColor(){ try { await this.shrinkImage() } catch (error) { console.log('error:' + error) } this.screeningThemeColor() } // 圖片縮小 async shrinkImage(){ var image = new Image(); image.src = this.imgUrl; await new Promise((resolve)=>{ image.onload = resolve }) let width = image.width let height = image.height let shrinkFactor = 10 let shrinkWidth = width / shrinkFactor let shrinkHeight = height / shrinkFactor let canvas = document.createElement('canvas') canvas.setAttribute('width',`${shrinkWidth}px`) canvas.setAttribute('height',`${shrinkHeight}px`) var ctx = canvas.getContext("2d") ctx.drawImage(image,0,0,shrinkWidth,shrinkHeight) this.shrinkUrl = canvas.toDataURL('image/jpeg',1) try { //保存像素 this.originalPiexls = ctx.getImageData(0,0,width,height) } catch (error) { console.log(error) } } // 開(kāi)始篩選主題色 screeningThemeColor(){ if(!this.originalPiexls || !this.originalPiexls.data || this.originalPiexls.data.length == 0){ throw('像素為空') } for(let i = 0;i < this.originalPiexls.data.length;i+=4){ let r = this.originalPiexls.data[i] let g = this.originalPiexls.data[i + 1] let b = this.originalPiexls.data[i + 2] let a = this.originalPiexls.data[i + 3] / 255.0 //添加一個(gè)色值范圍,讓它能忽略一定無(wú)效的像素值 if(a > 0 && (r < 200 && g < 200 && b < 200) && (r > 50 && g > 50 && b > 50)){ this.colorCountedSet.push(r,g,b,a) } } let maxCount = 0 // 尋找出現(xiàn)次數(shù)最多的像素定為主色調(diào) this.colorCountedSet.forEach((value,key)=>{ if(maxCount <= value){ maxCount = value this.themeColor = 'rgba(' + key + ')' } }) //執(zhí)行回調(diào) if(this.themeColorCallBack){ this.themeColorCallBack(this.shrinkUrl,this.themeColor) } } } // 統(tǒng)計(jì)不同像素的出現(xiàn)次數(shù) class ColorCountedSet{ //像素集合 map = new Map() //添加像素到集合 push(r,g,b,a){ //根據(jù)像素值生成一個(gè)map 元素 key值 let identification = r + ',' + g + ',' + b + ',' + a if(!this.map.get(identification)){ this.map.set(identification,1) } else { // 存在進(jìn)行次數(shù)自增 let times = parseInt(this.map.get(identification)) + 1 this.map.set(identification,times) } } // 給 ColorCountedSet 操作類添加一個(gè) forEach 方法 forEach(cb){ this.map.forEach(function(value,key){ cb(value,key) }); } }
三、總結(jié)與思考
內(nèi)容沒(méi)什么特別復(fù)雜的,之前做移動(dòng)端的時(shí)候有這么一個(gè)需求,其實(shí)實(shí)現(xiàn)的思路都是一樣的,這里就是一個(gè)拋磚引玉的作用,希望能給需要的人提供一個(gè)思路,如果能幫助大家,深感欣慰,代碼拙劣,大神勿笑[抱拳][抱拳][抱拳]
以上就是JavaScript實(shí)現(xiàn)簡(jiǎn)單獲取本地圖片主色調(diào)的詳細(xì)內(nèi)容,更多關(guān)于JavaScript獲取圖片主色調(diào)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
JS中的public和private對(duì)象,即static修飾符
先看下面的例子,它將告訴我們?cè)贘S世界中也有C#里的public , private ,及static等2012-01-01JavaScript鼠標(biāo)禁止右鍵禁止打開(kāi)控制臺(tái)及鍵盤禁用
這篇文章主要給大家介紹了關(guān)于JavaScript鼠標(biāo)禁止右鍵禁止打開(kāi)控制臺(tái)及鍵盤禁用的相關(guān)資料,實(shí)現(xiàn)禁止右鍵和禁止打開(kāi)控制臺(tái)是一種常見(jiàn)的網(wǎng)頁(yè)保護(hù)技巧,可以防止非法復(fù)制、盜取網(wǎng)頁(yè)資源等安全問(wèn)題,需要的朋友可以參考下2023-10-10JavaScript實(shí)現(xiàn)微信飛機(jī)大戰(zhàn)游戲
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)微信飛機(jī)大戰(zhàn)游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05深入學(xué)習(xí)JavaScript中的promise
這篇文章主要介紹了深入學(xué)習(xí)JavaScript中的promise,Promise對(duì)象的主要?途是通過(guò)鏈?zhǔn)秸{(diào)?的結(jié)構(gòu),將原本回調(diào)嵌套的異步處理流程,轉(zhuǎn)化成“對(duì)象.then().then()...”的鏈?zhǔn)浇Y(jié)構(gòu)2022-06-06WordPress中利用AJAX異步獲取評(píng)論用戶頭像的方法
這篇文章主要介紹了WordPress中利用AJAX異步獲取評(píng)論用戶頭像的方法,文中的例子是輸入郵箱即可獲取頭像,需要的朋友可以參考下2016-01-01