JavaScript如何实现简单获取本地图片主色调

其他教程   发布日期:2023年08月08日   浏览次数:575

本篇内容介绍了“JavaScript如何实现简单获取本地图片主色调”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

    实现

    1、实现思路

    其实思路很简单,就是将一张大图先缩小为一张小图,再遍历里面的像素,找到出现次数相对较高的一个;当然,先说明一下,这个也只能实现一个提取近似的值或者跟“人的意识”相反的值,因此,最终结果的“满意程度”可能不是很好。

    2、实现代码

    创建一个 ThemeColor 操作对象,通过回调返回缩略图主色调 ,可进行相关的其他操作

    1. //本地图片资源
    2. let url = 'tree.webp'
    3. document.getElementById('originalImage').src = url
    4. let themeColor = new ThemeColor(url,(shrinkUrl,color)=>{
    5. //缩略图
    6. let img = document.getElementById('showImage')
    7. if(img){
    8. img.setAttribute('src',shrinkUrl)
    9. }
    10. //主色
    11. document.getElementById('showDiv').style.backgroundColor = color
    12. })

    ThemeColor.js

    1. class ThemeColor{
    2. // 原图资源
    3. imgUrl = ''
    4. // 像素集合
    5. originalPiexls = null
    6. // 缩略图
    7. shrinkUrl = ''
    8. // 主色
    9. themeColor = 'white'
    10. // 回调
    11. themeColorCallBack = null
    12. // 提取像素出现最大次数操作对象
    13. colorCountedSet = new ColorCountedSet()
    14. constructor(imgUrl,callBack){
    15. this.imgUrl = imgUrl
    16. this.themeColorCallBack = callBack
    17. this.startScreeningThemeColor()
    18. }
    19. // 开始解析主色
    20. async startScreeningThemeColor(){
    21. try {
    22. await this.shrinkImage()
    23. } catch (error) {
    24. console.log('error:' + error)
    25. }
    26. this.screeningThemeColor()
    27. }
    28. // 图片缩小
    29. async shrinkImage(){
    30. var image = new Image();
    31. image.src = this.imgUrl;
    32. await new Promise((resolve)=>{
    33. image.onload = resolve
    34. })
    35. let width = image.width
    36. let height = image.height
    37. let shrinkFactor = 10
    38. let shrinkWidth = width / shrinkFactor
    39. let shrinkHeight = height / shrinkFactor
    40. let canvas = document.createElement('canvas')
    41. canvas.setAttribute('width',`${shrinkWidth}px`)
    42. canvas.setAttribute('height',`${shrinkHeight}px`)
    43. var ctx = canvas.getContext("2d")
    44. ctx.drawImage(image,0,0,shrinkWidth,shrinkHeight)
    45. this.shrinkUrl = canvas.toDataURL('image/jpeg',1)
    46. try {
    47. //保存像素
    48. this.originalPiexls = ctx.getImageData(0,0,width,height)
    49. } catch (error) {
    50. console.log(error)
    51. }
    52. }
    53. // 开始筛选主题色
    54. screeningThemeColor(){
    55. if(!this.originalPiexls || !this.originalPiexls.data || this.originalPiexls.data.length == 0){
    56. throw('像素为空')
    57. }
    58. for(let i = 0;i < this.originalPiexls.data.length;i+=4){
    59. let r = this.originalPiexls.data[i]
    60. let g = this.originalPiexls.data[i + 1]
    61. let b = this.originalPiexls.data[i + 2]
    62. let a = this.originalPiexls.data[i + 3] / 255.0
    63. //添加一个色值范围,让它能忽略一定无效的像素值
    64. if(a > 0 && (r < 200 && g < 200 && b < 200) && (r > 50 && g > 50 && b > 50)){
    65. this.colorCountedSet.push(r,g,b,a)
    66. }
    67. }
    68. let maxCount = 0
    69. // 寻找出现次数最多的像素定为主色调
    70. this.colorCountedSet.forEach((value,key)=>{
    71. if(maxCount <= value){
    72. maxCount = value
    73. this.themeColor = 'rgba(' + key + ')'
    74. }
    75. })
    76. //执行回调
    77. if(this.themeColorCallBack){
    78. this.themeColorCallBack(this.shrinkUrl,this.themeColor)
    79. }
    80. }
    81. }
    82. // 统计不同像素的出现次数
    83. class ColorCountedSet{
    84. //像素集合
    85. map = new Map()
    86. //添加像素到集合
    87. push(r,g,b,a){
    88. //根据像素值生成一个map 元素 key值
    89. let identification = r + ',' + g + ',' + b + ',' + a
    90. if(!this.map.get(identification)){
    91. this.map.set(identification,1)
    92. } else {
    93. // 存在进行次数自增
    94. let times = parseInt(this.map.get(identification)) + 1
    95. this.map.set(identification,times)
    96. }
    97. }
    98. // 给 ColorCountedSet 操作类添加一个 forEach 方法
    99. forEach(cb){
    100. this.map.forEach(function(value,key){
    101. cb(value,key)
    102. });
    103. }
    104. }

    以上就是JavaScript如何实现简单获取本地图片主色调的详细内容,更多关于JavaScript如何实现简单获取本地图片主色调的资料请关注九品源码其它相关文章!