欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript實(shí)現(xiàn)簡(jiǎn)單獲取本地圖片主色調(diào)

 更新時(shí)間:2023年03月07日 10:05:06   作者:頭疼腦脹的代碼搬運(yùn)工  
想象一個(gè)場(chǎng)景,就是如何根據(jù)一張圖片大概提取出它的主色調(diào)呢?獲取主色調(diào)后,可能會(huì)用來(lái)設(shè)置某些背景顏色,這里,利用?JS?簡(jiǎn)單獲取本地圖片主色調(diào),希望對(duì)大家有所幫助

一、實(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)文章

  • javascript 有用的腳本函數(shù)

    javascript 有用的腳本函數(shù)

    大家注意看下,感覺(jué)應(yīng)該是批量給網(wǎng)頁(yè)的一些標(biāo)簽加樣式的函數(shù)。
    2009-05-05
  • JS中的public和private對(duì)象,即static修飾符

    JS中的public和private對(duì)象,即static修飾符

    先看下面的例子,它將告訴我們?cè)贘S世界中也有C#里的public , private ,及static等
    2012-01-01
  • 使用JS讀取XML文件的方法

    使用JS讀取XML文件的方法

    由于項(xiàng)目上需要解析xml,于是各種百度,然后自己總結(jié)了下各個(gè)主流瀏覽器解析xml的方法,下面通過(guò)本文給大家詳細(xì)介紹下使用JS讀取XML文件的方法,一起看看吧
    2016-11-11
  • 使用js操作cookie的一點(diǎn)小收獲分享

    使用js操作cookie的一點(diǎn)小收獲分享

    js操作cookie想必大家并不陌生吧,本文積累了一點(diǎn)小經(jīng)驗(yàn),在此與大家分享下,希望對(duì)大家有所幫助
    2013-09-09
  • Javascript blur與click沖突解決辦法

    Javascript blur與click沖突解決辦法

    這篇文章主要介紹了Javascript blur與click沖突解決辦法的相關(guān)資料,在開(kāi)發(fā)過(guò)程中經(jīng)常會(huì)遇到blur與click 沖突的情況,這里舉了幾個(gè)例子,和解決辦法,需要的朋友可以參考下
    2017-01-01
  • JavaScript鼠標(biāo)禁止右鍵禁止打開(kāi)控制臺(tái)及鍵盤禁用

    JavaScript鼠標(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-10
  • JavaScript實(shí)現(xiàn)微信飛機(jī)大戰(zhàn)游戲

    JavaScript實(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

    這篇文章主要介紹了深入學(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-06
  • js實(shí)現(xiàn)一鍵復(fù)制功能

    js實(shí)現(xiàn)一鍵復(fù)制功能

    本文主要介紹了js實(shí)現(xiàn)一鍵復(fù)制功能的方法,具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧
    2017-03-03
  • WordPress中利用AJAX異步獲取評(píng)論用戶頭像的方法

    WordPress中利用AJAX異步獲取評(píng)論用戶頭像的方法

    這篇文章主要介紹了WordPress中利用AJAX異步獲取評(píng)論用戶頭像的方法,文中的例子是輸入郵箱即可獲取頭像,需要的朋友可以參考下
    2016-01-01

最新評(píng)論