var _____WB$wombat$assign$function_____=function(name){return (self._wb_wombat && self._wb_wombat.local_init && self._wb_wombat.local_init(name))||self[name];};if(!self.__WB_pmw){self.__WB_pmw=function(obj){this.__WB_source=obj;return this;}}{
let window = _____WB$wombat$assign$function_____("window");
let self = _____WB$wombat$assign$function_____("self");
let document = _____WB$wombat$assign$function_____("document");
let location = _____WB$wombat$assign$function_____("location");
let top = _____WB$wombat$assign$function_____("top");
let parent = _____WB$wombat$assign$function_____("parent");
let frames = _____WB$wombat$assign$function_____("frames");
let opens = _____WB$wombat$assign$function_____("opens");
function getColorFromImage(imageSrc){
  var image = new Image()
  image.crossOrigin = 'Anonymous'
  image.onload = onImageLoad
  image.src = imageSrc
}

function onImageLoad (ev) {
  var image = ev.target
  var canvas = document.createElement('canvas')
  // var width = 141, height = 115; // MD
  var width = 141, height = 141; // MD no crop
  canvas.width = width
  canvas.height = height
  
  var ctx = canvas.getContext('2d');
  ctx.drawImage(image, 0, 0, width, height, 0, 0, width, height);
  
  if (podcastExclusive) {
    removeOriginalsLogo(ctx, width, height)
  }
  
  var croppedImage = new Image()
  croppedImage.onload = getPalette
  croppedImage.src = canvas.toDataURL('image/png')
}

function removeOriginalsLogo (ctx, width, height) {
  var imageData = ctx.getImageData(0, 0, width, height);
  function isApproximate(value, to, tolerance) {
    return value > to-tolerance && value < to+tolerance;
  }
  for (var i=65892; i<imageData.data.length; i+=4) {
    if(isApproximate(imageData.data[i],255, 100) && isApproximate(imageData.data[i+1],128,100) && isApproximate(imageData.data[i+2],0,40) ){
      imageData.data[i]=255;
      imageData.data[i+1]=255;
      imageData.data[i+2]=255;
    }
    if(isApproximate(imageData.data[i],0, 50) && isApproximate(imageData.data[i+1],0,50) && isApproximate(imageData.data[i+2],0,50) ){
      imageData.data[i]=255;
      imageData.data[i+1]=255;
      imageData.data[i+2]=255;
    }
  }
  ctx.putImageData(imageData,0,0);
  ctx.beginPath();
  ctx.arc(36, 125, 11, 0, 2 * Math.PI, false)
  ctx.fillStyle = 'white'
  ctx.fill()
}

function getPalette(ev) {
  var image = ev.target
  Vibrant.from(image)
  .quality(0)
  .maxColorCount(32)
  .getPalette()
  .then((palette) => {
    var t = Object.keys(palette)
    .map(k => palette[k])
    .filter(v => v.hsl[1] > 0.3 && v.hsl[2] < 0.7 && v.population > 2 )
    .sort((a,b) => {return b.population-a.population})

    if (!t.length) {
      t = Object.keys(palette)
      .map(k => palette[k])
      .filter(v => v.bodyTextColor == '#fff' && v.population > 2)
      .sort((a,b) => {return b.population-a.population})
    }

    if (t.length) {
      bgColor = t[0].hex
      setBGColors(bgColor)
    }
  })
}

async function getImagePalette (imageSrc, quality = 0, maxColorCount = 32) {
  const promiseFunction = function (resolve, reject) {
    const onImageLoad = async function () {
      const canvas = document.createElement('canvas')
      let width = 141, height = 141; // MD no crop
      canvas.width = width
      canvas.height = height

      const ctx = canvas.getContext('2d');
      ctx.drawImage(image, 0, 0, width, height, 0, 0, width, height);
      removeOriginalsLogo(ctx, width, height)

      const onCroppedImageLoad = async function () {
        const palette = await Vibrant.from(croppedImage)
            .quality(quality)
            .maxColorCount(maxColorCount)
            .getPalette()
        resolve(palette)
      }

      const croppedImage = new Image()
      croppedImage.onload = onCroppedImageLoad
      croppedImage.src = canvas.toDataURL('image/png')
    }
    const image = new Image()
    image.crossOrigin = 'Anonymous'
    image.onload = onImageLoad
    image.src = imageSrc
  }
  return new Promise(promiseFunction)
}

function extractEmbedColors (palette) {
  const allColors = Object.keys(palette)
      .map(k => palette[k])

  let filteredColors = allColors.filter(v => v.hsl[1] > 0.3 && v.hsl[2] < 0.7 && v.population > 2)
  if (!filteredColors.length) {
    filteredColors = allColors.filter(v => v.bodyTextColor === '#fff' && v.population > 2)
  }
  return filteredColors.sort((a, b) => b.population - a.population).map(c => c.hex)
}
}