角度5,从图像获取颜色

Pat*_*ogh 0 android android-palette angular5

我有一个使用android.support.v7.graphics.Palette的android应用程序,可从动态图像获取颜色信息,然后自定义活动的布局,以使用图像中的突变颜色。我的问题是,Angular 5有什么相似之处吗?我想将此项目的Web版本建模为尽可能接近android版本。这将意味着在选择图像后动态设置一些样式颜色。

更新:我一直在寻找JavaScript的ColorThief()。但是我不确定如何从Angular 5组件中访问它。

谢谢PK

Pat*_*ogh 6

对于寻找这种东西的人,我最终使用了node-vibrant.js。我运行npm install,然后将该文件添加到angular.json文件中的脚本数组中

"scripts": [
  "node_modules/node-vibrant/dist/vibrant.min.js"
]
Run Code Online (Sandbox Code Playgroud)

然后,我将tsconfig.json文件更改为以下内容:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "allowSyntheticDefaultImports": true,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "module": "commonjs",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  },
  "allowJs": true
}
Run Code Online (Sandbox Code Playgroud)

然后,我将Vibrant和Palette导入到我的angular 6.0.0组件中

import Vibrant from 'node-vibrant';
import { Palette } from 'node-vibrant/lib/color';
Run Code Online (Sandbox Code Playgroud)

然后,仅用图像的URL即可调用ngOnInit()相当容易

getVibrantColor(url: string){
  // Using builder
  Vibrant.from(url).getPalette((err, palette) => {
    console.log(palette)
    this.palette = palette;
  });

}

styleContainer(): any {
  console.log('palette', this.palette);
  if (this.palette.LightVibrant) {
    return { 'background-color': this.palette.LightVibrant.getHex(), 'border-color': 
      this.palette.LightMuted.getHex(), 'color': '#000000' };
  } else {
    return { 'background-color': '#FFFFFF', 'border-color':        
       this.palette.LightMuted.getHex(), 'color': '#000000' };
  }

}
Run Code Online (Sandbox Code Playgroud)

并从html文件中调用它:

<div *ngIf="palette" class="col-lg-8 details-top-container" 
[ngStyle]="styleContainer()"></div>
Run Code Online (Sandbox Code Playgroud)

最困难的部分是获取正确的import语句。

import * as Vibrant from 'node-vibrant';
Run Code Online (Sandbox Code Playgroud)

不能像文档所说的那样工作。

希望这可以节省一些时间。

PK