Create a chessboard texture
@param  {number} [size]
@param  {number} [unitSize]
@param  {string} [color1]
@param  {string} [color2]
@return {clay.Texture2D}
506978
function (size, unitSize, color1, color2) {
        size = size || 512;
        unitSize = unitSize || 64;
        color1 = color1 || 'black';
        color2 = color2 || 'white';

        var repeat = Math.ceil(size / unitSize);

        var canvas = document.createElement('canvas');
        canvas.width = size;
        canvas.height = size;
        var ctx = canvas.getContext('2d');
        ctx.fillStyle = color2;
        ctx.fillRect(0, 0, size, size);

        ctx.fillStyle = color1;
        for (var i = 0; i < repeat; i++) {
            for (var j = 0; j < repeat; j++) {
                var isFill = j % 2 ? (i % 2) : (i % 2 - 1);
                if (isFill) {
                    ctx.fillRect(i * unitSize, j * unitSize, unitSize, unitSize);
                }
            }
        }

        var texture = new Texture2D({
            image: canvas,
            anisotropic: 8
        });

        return texture;
    }
218393
public Texture createTexture (float width, float height, Texture.Config config) {
    int texWidth = config.toTexWidth(scale.scaledCeil(width));
    int texHeight = config.toTexHeight(scale.scaledCeil(height));
    if (texWidth <= 0 || texHeight <= 0) throw new IllegalArgumentException(
      "Invalid texture size: " + texWidth + "x" + texHeight);

    int id = createTexture(config);
    gl.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight,
                    0, GL_RGBA, GL_UNSIGNED_BYTE, null);
    return new Texture(this, id, config, texWidth, texHeight, scale, width, height);
  }
517424
function colorBlend(mode, color1, color2) {
    var ab = color1.alpha, cb, // backdrop
        as = color2.alpha, cs, // source
        ar, cr, r = [];        // result

    ar = as + ab * (1 - as);
    for (var i = 0; i < 3; i++) {
        cb = color1.rgb[i] / 255;
        cs = color2.rgb[i] / 255;
        cr = mode(cb, cs);
        if (ar) {
            cr = (as * cs + ab * (cb
                - as * (cb + cs - cr))) / ar;
        }
        r[i] = cr * 255;
    }

    return new(tree.Color)(r, ar);
}
1228032
public static SVGGraphics2D createSvgGraphics(final Dimension size)
            throws ParserConfigurationException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.getDOMImplementation().createDocument(null, "svg", null);

        SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(document);
        ctx.setStyleHandler(new OpacityAdjustingStyleHandler());
        ctx.setComment("Generated by GeoTools2 with Batik SVG Generator");

        SVGGraphics2D g2d = new SVGGraphics2D(ctx, true);
        g2d.setSVGCanvasSize(size);

        return g2d;
    }
193557
function() {
		this.gl = this.context.gl; 
		var gl = this.gl;

		this.id = gl.createTexture(); //texture ID is recreated
		this.width = this.height = 0; //size is reset to zero until loaded
		this.target = gl.TEXTURE_2D;  //the provider can change this if necessary (e.g. cube maps)

		this.bind();

		//TODO: clean these up a little. 
		gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, Texture.UNPACK_PREMULTIPLY_ALPHA);
		gl.pixelStorei(gl.UNPACK_ALIGNMENT, Texture.UNPACK_ALIGNMENT);
		gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, Texture.UNPACK_FLIP_Y);
		
		var colorspace = Texture.UNPACK_COLORSPACE_CONVERSION || gl.BROWSER_DEFAULT_WEBGL;
		gl.pixelStorei(gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, colorspace);

		//setup wrap modes without binding redundantly
		this.setWrap(this.wrapS, this.wrapT, false);
		this.setFilter(this.minFilter, this.magFilter, false);
		
		if (this.managedArgs) {
			this.setup(this.managedArgs);
		}
	}
184035
function (color1, color2, weight) {
        if (!weight) {
            weight = new(tree.Dimension)(50);
        }
        var p = weight.value / 100.0;
        var w = p * 2 - 1;
        var a = color1.toHSL().a - color2.toHSL().a;

        var w1 = (((w * a == -1) ? w : (w + a) / (1 + w * a)) + 1) / 2.0;
        var w2 = 1 - w1;

        var rgb = [color1.rgb[0] * w1 + color2.rgb[0] * w2,
                   color1.rgb[1] * w1 + color2.rgb[1] * w2,
                   color1.rgb[2] * w1 + color2.rgb[2] * w2];

        var alpha = color1.alpha * p + color2.alpha * (1 - p);

        return new(tree.Color)(rgb, alpha);
    }
1307211
function LGraphTextureMix() {
            this.addInput("A", "Texture");
            this.addInput("B", "Texture");
            this.addInput("Mixer", "Texture");

            this.addOutput("Texture", "Texture");
            this.properties = { factor: 0.5, precision: LGraphTexture.DEFAULT };
            this._uniforms = {
                u_textureA: 0,
                u_textureB: 1,
                u_textureMix: 2,
                u_mix: vec4.create()
            };
        }
477649
function initColors() {
	var low, high,
	    result = new Uint8Array( 0x1000000 );

	for ( low = 0; low < 0x100; low++ ) {
		for ( high = 0; high < 0x100; high++ ) {
			addTile( low, high );
		}
	}

	return result;

	function addTile( low, high ) {
		var colorLow, colorHigh, color,
		    offset = ( low << 16 ) | ( high << 8 );

		for ( var i = 0; i < 8; i++ ) {
			colorLow = ( low & 1 );
	    	colorHigh = ( high & 1 ) << 1;
	    	color = ( colorHigh | colorLow );

			result[ offset + i ] = color;

			low >>= 1;
	    	high >>= 1;
		}
	}
}
899831
function(ctx, size, texMeters) {
    // fill all with ground Color
    ctx.fillStyle = this.data.groundColor;
    ctx.fillRect(0, 0, size, size);

    var i, col, col1, col2, im, imdata, numpixels;

    if (this.data.groundTexture == 'none') return;
    switch(this.data.groundTexture) {
      case 'checkerboard': {
        ctx.fillStyle = this.data.groundColor2;
        var num = Math.floor(texMeters / 2);
        var step = size / (texMeters / 2); // 2 meters == <step> pixels
        for (i = 0; i < num + 1; i += 2) {
          for (var j = 0; j < num + 1; j ++) {
            ctx.fillRect(Math.floor((i + j % 2) * step), Math.floor(j * step), Math.floor(step), Math.floor(step));
          }
        }
        break;
      }
      case 'squares': {
        var numSquares = 16;
        var squareSize = size / numSquares;
        col1 = new THREE.Color(this.data.groundColor);
        col2 = new THREE.Color(this.data.groundColor2);
        for (i = 0; i < numSquares * numSquares; i++) {
          col = this.random(i + 3) > 0.5 ? col1.clone() : col2.clone();
          col.addScalar(this.random(i + 3) * 0.1 - 0.05);
          ctx.fillStyle = '#' + col.getHexString();
          ctx.fillRect((i % numSquares) * squareSize, Math.floor(i / numSquares) * squareSize, squareSize, squareSize);
        }
        break;
      }
      case 'noise': {
      // TODO: fix
        imdata = ctx.getImageData(0, 0, size, size);
        im = imdata.data;
        col1 = new THREE.Color(this.data.groundColor);
        col2 = new THREE.Color(this.data.groundColor2);
        var diff = new THREE.Color(col2.r - col1.r, col2.g - col1.g, col2.b - col1.b);
        var perlin = new PerlinNoise();
        for (i = 0, j = 0, numpixels = im.length; i < numpixels; i += 4, j++){
          //console.log( (j % size) / size, j / size)
          var rnd = perlin.noise((j % size) / size * 3, j / size / size * 3, 0);
          im[i + 0] = Math.floor((col1.r + diff.r * rnd) * 255);
          im[i + 1] = Math.floor((col1.g + diff.g * rnd) * 255);
          im[i + 2] = Math.floor((col1.b + diff.b * rnd) * 255);
        }
        ctx.putImageData(imdata, 0, 0);
        break;
      }
      case 'walkernoise': {
        var s = Math.floor(size / 2);
        var tex = document.createElement('canvas');
        tex.width = s;
        tex.height = s;
        var texctx = tex.getContext('2d');
        texctx.fillStyle = this.data.groundColor;
        texctx.fillRect(0, 0, s, s);
        imdata = texctx.getImageData(0, 0, s, s);
        im = imdata.data;
        col1 = new THREE.Color(this.data.groundColor);
        col2 = new THREE.Color(this.data.groundColor2);
        var walkers = [];
        var numwalkers = 1000;
        for (i = 0; i < numwalkers; i++) {
          col = col1.clone().lerp(col2, Math.random());
          walkers.push({
            x: Math.random() * s,
            y: Math.random() * s,
            r: Math.floor(col.r * 255),
            g: Math.floor(col.g * 255),
            b: Math.floor(col.b * 255)
          });
        }
        var iterations = 5000;
        for (var it = 0; it< iterations; it++){
          for (i = 0; i < numwalkers; i++) {
            var walker = walkers[i];
            var pos = Math.floor((walker.y * s + walker.x)) * 4;
            im[pos + 0] = walker.r;
            im[pos + 1] = walker.g;
            im[pos + 2] = walker.b;
            walker.x += Math.floor(Math.random() * 3) - 1;
            walker.y += Math.floor(Math.random() * 3) - 1;
            if (walker.x >= s) walker.x = walker.x - s;
            if (walker.y >= s) walker.y = walker.y - s;
            if (walker.x < 0) walker.x = s + walker.x;
            if (walker.y < 0) walker.y = s + walker.y;
          }
        }
        texctx.putImageData(imdata, 0, 0);
        ctx.drawImage(tex, 0, 0, size, size);
        break;
      }
    }
  }
979237
private void computeSheet(Map<TileRef, ColorRgba> colors, Integer sheet)
    {
        final SpriteTiled tiles = map.getSheet(sheet);
        final ImageBuffer tilesSurface = tiles.getSurface();
        final int tw = map.getTileWidth();
        final int th = map.getTileHeight();

        int number = 0;
        for (int i = 0; i < tilesSurface.getWidth(); i += tw)
        {
            for (int j = 0; j < tilesSurface.getHeight(); j += th)
            {
                final int h = number * tw % tiles.getWidth();
                final int v = number / tiles.getTilesHorizontal() * th;
                final ColorRgba color = UtilColor.getWeightedColor(tilesSurface, h, v, tw, th);

                if (!(NO_TILE.equals(color) || color.getAlpha() == 0))
                {
                    colors.put(new TileRef(sheet, number), color);
                }
                number++;
            }
        }
    }
1206997
function Texture2D (opts) {
    var file    = opts.file
      , url     = opts.url
      , data    = opts.data
      , texture = opts.texture

    if (url) {
        this.name = url
        data = new Image()
        data.src = url
    } else if (file) {
        this.name = file
        data = resource(file)
    } else if (texture) {
        this.name = texture.name
        data = texture.imgElement
    }

    this.size = {width: 0, height: 0}

    if (data instanceof RemoteResource) {
        events.addListenerOnce(data, 'load', this.dataDidLoad.bind(this, data))
        this.imgElement = data.load()
    } else if (url) {
        this.imgElement = data
        this.imgElement.onload = this.dataDidLoad.bind(this, data)
    } else {
        this.imgElement = data
        this.dataDidLoad(data)
    }
}
726952
function _drawLine(v0, v1, color) {
        var p = new Primitive();

        p.vertices = [v0, v1];
        p.color = toColor(color);

        renderer.addPrimitive(p);
    }
100471
function inerpolateColors(col1, col2, amount) {
  let col1rgb = parseColor(col1);
  let col2rgb = parseColor(col2);
  return RGBobj2string({
    r: Math.min(255, col1rgb.r * amount + col2rgb.r * (1-amount)),
    g: Math.min(255, col1rgb.g * amount + col2rgb.g * (1-amount)),
    b: Math.min(255, col1rgb.b * amount + col2rgb.b * (1-amount)),
    a: Math.min(1, col1rgb.a * amount + col2rgb.a * (1-amount))
  });
}
867598
function getMaterialDiff(pieces) {
    const diff = {
      white: {
        king: 0, queen: 0, rook: 0, bishop: 0, knight: 0, pawn: 0,
      },
      black: {
        king: 0, queen: 0, rook: 0, bishop: 0, knight: 0, pawn: 0,
      },
    };

    Object.keys(pieces).forEach((k) => {
      const p = pieces[k];
      const them = diff[opposite(p.color)];
      const i = 1;
      if (them[p.role] > 0) {
        them[p.role] -= i;
      } else {
        diff[p.color][p.role] += i;
      }
    });

    return diff;
  }
714826
function create2x2(a, b, c, d){
    if (undef(a)) throw new Error("At least one argument must be provided.");

    if (undef(b)){
        // Everything = a
        b = a;
        c = a;
        d = a;
    }
    else{
        // we have a and b
        if(undef(c)){
            c = a;
            d = b;
        }
        else{
            if(undef(d)) throw new Error("Argument 'd' must be provided if 'c' is.");
        }
    }

    return new Vector([[a, b],[c, d]]);
}
157072
public Tile tile (float x, float y, float width, float height) {
    final float tileX = x, tileY = y, tileWidth = width, tileHeight = height;
    return new Tile() {
      @Override public Texture texture () { return Texture.this; }
      @Override public float width () { return tileWidth; }
      @Override public float height () { return tileHeight; }
      @Override public float sx () { return tileX/displayWidth; }
      @Override public float sy () { return tileY/displayHeight; }
      @Override public float tx () { return (tileX+tileWidth)/displayHeight; }
      @Override public float ty () { return (tileY+tileWidth)/displayHeight; }
      @Override public void addToBatch (QuadBatch batch, int tint, AffineTransform tx,
                                        float x, float y, float width, float height) {
        batch.addQuad(texture(), tint, tx, x, y, width, height, tileX, tileY, tileWidth, tileHeight);
      }
      @Override public void addToBatch (QuadBatch batch, int tint, AffineTransform tx,
                                        float dx, float dy, float dw, float dh,
                                        float sx, float sy, float sw, float sh) {
        batch.addQuad(texture(), tint, tx, dx, dy, dw, dh, tileX+sx, tileY+sy, sw, sh);
      }
    };
  }
1350901
def _mix(color1, color2, weight=0.5, **kwargs):
    
    weight = float(weight)
    c1 = color1.value
    c2 = color2.value
    p = 0.0 if weight < 0 else 1.0 if weight > 1 else weight
    w = p * 2 - 1
    a = c1[3] - c2[3]

    w1 = ((w if (w * a == -1) else (w + a) / (1 + w * a)) + 1) / 2.0
    w2 = 1 - w1
    q = [w1, w1, w1, p]
    r = [w2, w2, w2, 1 - p]
    return ColorValue([c1[i] * q[i] + c2[i] * r[i] for i in range(4)])