Home | History | Annotate | Download | only in effects
      1 /*
      2  * Copyright 2011 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #include "SkColorMatrixFilter.h"
      9 
     10 static SkScalar byte_to_scale(U8CPU byte) {
     11     if (0xFF == byte) {
     12         // want to get this exact
     13         return 1;
     14     } else {
     15         return byte * 0.00392156862745f;
     16     }
     17 }
     18 
     19 SkColorFilter* SkColorMatrixFilter::CreateLightingFilter(SkColor mul, SkColor add) {
     20     if (0 == add) {
     21         return SkColorFilter::CreateModeFilter(mul | SK_ColorBLACK,
     22                                                SkXfermode::Mode::kModulate_Mode);
     23     }
     24     SkColorMatrix matrix;
     25     matrix.setScale(byte_to_scale(SkColorGetR(mul)),
     26                     byte_to_scale(SkColorGetG(mul)),
     27                     byte_to_scale(SkColorGetB(mul)),
     28                     1);
     29     matrix.postTranslate(SkIntToScalar(SkColorGetR(add)),
     30                          SkIntToScalar(SkColorGetG(add)),
     31                          SkIntToScalar(SkColorGetB(add)),
     32                          0);
     33     return SkColorMatrixFilter::Create(matrix);
     34 }
     35