8-bit Chess Pixel Counting
C#6, 107 bytes
Here's my own answer. I don't expect any points being the one who posed the challenge.
I have taken some inspiration from user81655's answer.
long P(string a)=>(a[0]>99?12201284685:11042628752)+(a[2]+a[3])%2*46566348643>>"KQBNRP".IndexOf(a[1])*6&63;
The pixel counts are encoded in 6-bit blocks. The outline or fill is added to the square (if it's white). Finally, the 6-bit block for the appropriate piece is extracted.
Thankfully, operation precedence worked heavily in my favor.
JavaScript (ES6), 106
As an anonymous function.
x=>(o=+'137999'[p='PNKBQR'.search(x[1])],f=+'262149'[p]+p,(parseInt(x[2]+x[3],19)%2?9:55-f-o)+(x>'w'?f:o))
By now, I'm following the simplest way of find the answer with a calculation - this could be not the best way.
Over a black square, the answer is the size of fill for white pieces and the size of outline for black pieces. Over a white square, you need to add the free space. See the table below (inside the snippet)
I keep the size of fill and outline for each piece, the free space inside the square can be found subtracting from 64. To save space, the outline in stored as a single digit afer subtracing 9. The fill is trickier as the range is wider, check the code (that's way the pieces are sorted by occupied space)
Test snippet:
F=x=>(
o=+'137999'[p='PNKBQR'.search(x[1])], // get outline - 9
f=+'262149'[p]+p, // get fill -9
(
parseInt(x[2]+x[3],19) // parse with an odd base the differentiate between odd and even rows
%2?9:55-f-o // black square if odd,, white if even so calc free space
) +(x>'w'?f:o) // add fill or outline based on piece color
)
// Test suite
console.log=x=>O.innerHTML+=x+'\n'
for(i=0; z='PNKBQR'[i]; i++)
{
o = '';
t = 'w'+z+'c2'; // white piece, white square
o += t+' '+F(t)+', '
t = 'b'+z+'c2'; // black piece, white square
o += t+' '+F(t)+', '
t = 'w'+z+'a1'; // white piece, black square
o += t+' '+F(t)+', '
t = 'b'+z+'a1'; // black piece, black square
o += t+' '+F(t)
console.log(o);
}
<pre>
Piece Fill Outline Free w/w b/w w/b b/b
=============================================
Pawn 11 10 43 54 53 11 10
Knight 16 12 36 52 48 16 12
King 13 16 35 48 51 13 16
Bishop 13 18 33 46 51 13 18
Queen 17 18 29 46 47 17 18
Rook 23 18 23 46 41 23 18
</pre>
<pre id=O></pre>
JavaScript (ES6), 135 112 bytes
s=>(c={K:`\u000a\u0010\u0023`,Q:`\u0011\u0012\u001d`,B:`\u000a\u0012\u0021`,N:`\u0010\u000c\u0024`,R:`\u0017\u0012\u0017`,P:`\u000b\u000a\u002b`}[s[1]])[f="charCodeAt"](s<"w")+((s[f](2)-s[3])%2&&c[f](2))
Every \u00xx
should be a single one-byte character. They are represented here as codes because Stack Exchange automatically removes unreadable characters from posts.
Explanation
s=>
// c = string of three (mostly unreadable) characters, the ASCII code of each character
// represents the number of pixels in the fill, outline and square respectively
(c={
K:`\u000a\u0010\u0023`,
Q:`\u0011\u0012\u001d`,
B:`\u000a\u0012\u0021`,
N:`\u0010\u000c\u0024`,
R:`\u0017\u0012\u0017`,
P:`\u000b\u000a\u002b`
}[s[1]])
[f="charCodeAt"](s<"w") // if piece is black add outline pixels, else add fill pixels
+((s[f](2)-s[3])%2 // this returns 1 if the square is white or 0 if black
&&c[f](2)) // if the square is white add the square's pixels
Test
var solution = s=>(c={K:`\u000a\u0010\u0023`,Q:`\u0011\u0012\u001d`,B:`\u000a\u0012\u0021`,N:`\u0010\u000c\u0024`,R:`\u0017\u0012\u0017`,P:`\u000b\u000a\u002b`}[s[1]])[f="charCodeAt"](s<"w")+((s[f](2)-s[3])%2&&c[f](2))
<input type="text" id="input" value="bKg8" />
<button onclick="result.textContent=solution(input.value)">Go</button>
<pre id="result"></pre>